Creating Altering and Deleting Tables in SQL server
Creating Altering and Deleting Tables in SQL server
1. What is SQL?
2. Understanding SQL Sub Languages
3. Data Definition Language in SQL Server.
4. Create Command in SQL Server
5. Alter Command in SQL Server
6. Truncate Command in SQL Server.
7. Drop Command in SQL Server.
8. What are the differences between the delete and truncate
commands in SQL Server?
What is SQL?
The CREATE command is used to create a new database object in a database such
as tables, views, functions, etc. In SQL Server, all database objects (tables, views,
etc) are saved with an extension of “dbo.<object name>”. The syntax to create a
database is shown below.
This command is used to change or modify the structure of a table. In SQL Server,
using the ALTER command we can perform the following operations on an
existing table.
Example:
To understand the use of ALTER command in SQL Server, let’s first create the
following Student table.
ALTER-ALTER COLUMN:
This command is used to change a data type from an old data type to a new data
type and also to change the size of a data type of a column.
Syntax: ALTER TABLE <TABLENAME> ALTER COLUMN <COLUMNNAME> <NEW
DATA TYPE>[NEW SIZE]
In our student table, the column Name width is VARCHAR(50). Let’s change the
width to VARCHAR (100). To do so, we need to use the Alter command as shown
below.
Note: When you increase the width of a column, you won’t face any problem but
while decreasing the width if the table contains data in it we cannot decrease the
width less than the max existing characters in the column.
If you want to change the data type of an existing column, then you can use the
ALTER command. For example, currently, the Name column data type is VARCHAR
and our requirement is to change the data type from VARCHAR to NVARCHAR. To
do so, you need to use the ALTER command as shown below.
If you want to change a NULL column to NOT NULL then you can use the ALTER
command. When you create a column without NULL or NOT NULL constraint,
then by default it is NULL. It means this column can accept NULL values. Suppose,
you want to change the No column from NULL to NOT NULL, then you need to use
the ALTER Command as shown below.
If you want to change a NOT NULL column to NULL then also you can use the
ALTER command. For example, if you want to change the No column from NOT
NULL to NULL, then you can use the ALTER Command as shown below.
If you want to add a new column to an existing table, then you can use the ALTER
Command. The syntax to use the command to add a new column is given below.
Suppose, you want to add the Branch column to the existing Student table. Then
you need to use the ALter command as shown below.
If you want to delete an existing column from a table in SQL Server, then you
need to use the ALTER Command. The syntax to use the ALTER command to
delete or drop a column is given below.
For example, if you want to delete or drop the Brach column from the Student
table, then you need to use the ALTER command as shown below.
ALTER TABLE Student DROP COLUMN Branch
Suppose, you want to change the name column from Name to StudentName,
then you need to use this stored procedure as shown below.
This SP_RENAME stored procedure can also be used to change a table name from
the old table name to a new name. The syntax to change the table name using
SP_RENAME stored procedure is given below.
Suppose, you want to delete all the records from the Student table, then you
need to use the TRUNCATE command as shown below in SQL Server.
TRUNCATE TABLE Student
Note: The truncate command will delete rows but not the structure of the table.
Delete supports WHERE clause. Truncate does not support the WHERE claus
Delete supports rollback transactions for Truncate doesn’t support rollback transactio
restoring the deleted data. cannot restore the deleted information