Professional Documents
Culture Documents
Submitted By:ali Ishtiaq Roll No: 6257 Cs6: Create
Submitted By:ali Ishtiaq Roll No: 6257 Cs6: Create
Roll no : 6257
Cs 6th
Scenario:
Creating Table of Books and implementing all DDL commands
CREATE
The CREATE query is used to create a database or objects such as tables, views, stored procedures, etc.
Creating a table
The CREATE query is also used to add tables in an existing database as shown in the following script:
1 USE LibraryDB
3(
7)
The above script creates a table named “Books” in the “LibraryDB” database that we created earlier.
The “Books” table contains three columns: Id, Name, and Price. The Id column is the primary key column
and it cannot be NULL. A column with a PRIMARY KEY constraint must contain unique values.
However, since we have set the IDENTITY property for the Id column, every time a new record is added
in the Books table, the value of the Id column will be incremented by 1, starting from 1. You need to
specify the values for the Name column as well as it cannot have NULL. Finally, the Price column can
have NULL values.
To view all the tables in the LibraryDB, execute the following QL DDL script:
1 USE LibraryDB
2 GO
4 GO
Similarly, to see all the columns in the Books table, run the following script:
2 FROM INFORMATION_SCHEMA.COLUMNS
3 WHERE TABLE_NAME='Books'
ALTER
The ALTER command in SQL DDL is used to modify the structure of an already existing table.
For example, if we want to add a new column e.g. ISBN to the existing Books table in the LibraryDB
database, the ALTER command can be used as follows:
1 USE LibraryDB
The syntax of the ALTER command is straightforward. The ALTER statement is used followed by the
object type and the name of the object, which in this case are TABLE and Books, respectively.
Next, you need to specify the operation that you need to perform, which is ADD in our case. Let’s now
again SELECT the columns from the Books table and see if the ISBN column has been added to the
Books table:
2 FROM INFORMATION_SCHEMA.COLUMNS
3 WHERE TABLE_NAME='Books'
Let’s see another case where ALTER command can be useful. Suppose, instead of adding a new column
to a table, you want to modify an existing column. For example, you want to change the data type of the
ISBN column from INT to VARCHAR (50). The ALTER query can be used as follows:
1 USE LibraryDB
You can see that to modify an existing column within a table, we first have to use the ALTER command
with the table name and then another ALTER command with the name of the column that is to be
modified.
If you again select the column names, you will see the updated data type (VARCHAR) for the ISBN
column.
DROP
The DROP command is a type of SQL DDL command, that is used to delete an existing database or an
object within a database.
Deleting a database
The following DROP command deletes the LibraryDB database that we created earlier:
Note: If you execute the above command, the LibraryDB database will be deleted. To execute the rest of
the queries in this article, you will again need to CREATE the LibraryDB database, along with the Books
table.
Deleting a table
The DROP command is a type of SQL DDL command that is used to delete an existing table. For
instance, the following command will delete the Books table:
Deleting a column
To delete a column within a database, the DROP query is used in combination with the ALTER query.
The ALTER query specifies the table that you want to delete whereas the DROP query specifies the
column to delete within the table specified by the ALTER query. Let’s drop the ISBN column from the
Books:
TRUNCATE
The TRUNCATE command in SQL DDL is used to remove all the records from a table. Let’s insert a few
records in the Books table:
3 ('Book-B', 200),
4 ('Book-C', 150)
The TRUNCATE command will remove all the records from the Books table as shown below:
If you again select all the records from the Books table, you will see that the table is empty.