In SQL Server, a table is a fundamental database object that stores data in a structured format. It is used to organize and group related information in a way that makes it easy to access and manage. A table consists of columns, which define the properties of the data being stored, and rows, which contain the actual data values.
Tables in SQL Server are used to store a wide variety of data, such as customer information, product details, financial records, and more. They are created using a CREATE TABLE statement, which specifies the column names, data types, and any constraints or rules that must be followed when inserting or updating data.
For example, the following SQL code creates a simple table called ‘Employees‘ with three columns: ‘EmployeeID‘, ‘FirstName‘, and ‘LastName‘.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50)
);Once a table has been created, data can be inserted into it using an INSERT statement. For example, the following SQL code inserts a new employee record into the ‘Employees‘ table:
INSERT INTO Employees (EmployeeID, FirstName, LastName)
VALUES (1, 'John', 'Doe');Data can also be retrieved from a table using a SELECT statement, which retrieves one or more columns from one or more rows in the table. For example, the following SQL code retrieves the first name and last name of all employees in the ‘Employees‘ table:
SELECT FirstName, LastName
FROM Employees;Tables can also be modified using ALTER TABLE statements to add or remove columns or constraints, and can be dropped using a DROP TABLE statement to delete the entire table and its data.
Overall, tables are an important and essential component of SQL Server databases, allowing data to be stored, organized, and manipulated in a structured and efficient manner.