There are no items in your cart
Add More
Add More
Item Details | Price |
---|
Sat Apr 20, 2024
In SQL Server, A table is a database object that stores and organizes data in a structured manner. It consists of rows and columns, where each row represents a single record of data and each column represents a specific attribute or field of that record.
Each column is assigned a specific data type that determines the type of data it can accommodate such as numbers, strings, or temporal data.
Let’s learn how we can create a table in SQL Server
There are two ways to create a new table –
1. Using T-SQL Script
2. Using Table Designer
Create table using T-SQL Script
Create a Database
In case if you haven’t created a database yet, you need to create a database first where the table will be stored. You can use the below Query for creating a database. And, after creating a database we have to use database, so we will use the USE command.
Create a Table using T-SQL Script: Now, we will create a table under the Demo_database.
Table named “Employees” with columns for Employee ID, first name, last name, email, age, and city. The CustomerID column is set as the primary key.
Use below syntax for creating a table.
OR
We can also create a table using table designer in SSMS
Here’s an example of how to create a table using the Table Designer in SQL Server:
We can also insert multiple Rows at a time, as shown in below query –
Result:
Alter Table:
In SQL Server, the ALTER TABLE statement is used to modify the structure of an existing table. Here are some examples of how to use the ALTER TABLE statement:
The syntax for adding a new column is as follows:
Example: Add one more column named Employee Salary in Employees table, which table we have created.
Result:
Here you can see the new column ‘Salary’, has been added.
Using ALTER command, we can also change column’s data type. We can do this by using following syntax:
For example, to change the data type of a column named “Salary” in a table named “Employees” from MONEY to DECIMAL(10, 2), you can use the following command:
As you can see in above images, before modifying the column’s data type it was MONEY and after modifying data type into decimal it is showing decimal data type. Same way you can change the data type of a column.
For example, to rename a column named “City” to “Location” in a table named “Employees”, you can use the following command:
EXEC sp_rename ‘Employees.City’, ‘Location’, ‘COLUMN’;
Result:
For example, to add a primary key constraint named “PK_Employees ” to a table named “Employees” on a column named “EmpID”, you can use the following command:
Syntax for Adding a Foreign Key:
Now, to add the foreign key constraint to the “Orders” table using ALTER TABLE, you can use the following SQL code:
In this example, we’re using the ALTER TABLE statement to add a foreign key constraint to the “Orders” table. The CONSTRAINT keyword is used to specify the name of the foreign key constraint (in this case, “FK_Orders_Customers”). The FOREIGN KEY keyword is used to specify that we’re adding a foreign key constraint. The CustomerID column in the “Orders” table is then specified as the column that the foreign key constraint applies to, and the REFERENCES keyword is used to specify the table and column that the foreign key references (in this case, “Customers” table and “CustomerID” column)Once you run the above SQL code, the foreign key constraint will be added to the “Orders” table, and you’ll be able to enforce referential integrity between the two tables.
Example: Remove a foreign key constraint named “FK_Orders_Customers” from a table named “Orders”.
you can use the following command:For example, to drop a column named “OrderDate” from a table named “Orders”, you can use the following command:
Drop table named “Orders”.
DROP TABLE Orders;
Update Table:The UPDATE statement is used to modify the existing records in a table.As we have a table named “Employees”, let’s modify its record using UPDATE statement.For example: Update the Location as “London” whose Employee ID is 1.Syntax:
In the same way you can update any record using UPDATE statement.While updating data in SQL Server using the UPDATE statement, there are a few points to keep in mind:
Vijay Kashyap
SQL in simplified manner