Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Submitted by :Ali Ishtiaq

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.

1 CREATE DATABASE LibraryDB

The script above creates a database named “LibraryDB” in MS SQL Server.

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

2 CREATE TABLE Books

3(

4 Id INT PRIMARY KEY IDENTITY(1,1),

5 Name VARCHAR (50) NOT NULL,


6 Price INT

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

3 SELECT * FROM INFORMATION_SCHEMA.TABLES

4 GO

You should see the following output:

Similarly, to see all the columns in the Books table, run the following script:

1 SELECT COLUMN_NAME, DATA_TYPE

2 FROM INFORMATION_SCHEMA.COLUMNS

3 WHERE TABLE_NAME='Books'

Here is the output:


You can see how the CREATE query can be used to define the structure of a table and the type of data
that will be stored in a table. Note, we have not added any record to the Books table yet as SQL DDL
commands are only concerned with the structure of the database and not with the database records. The
SQL DML commands are used for inserting, modifying and deleting database records.

ALTER
The ALTER command in SQL DDL is used to modify the structure of an already existing table.

Adding a new column

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

2 ALTER TABLE Books

3 ADD ISBN INT NOT NULL;

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:

1 SELECT COLUMN_NAME, DATA_TYPE

2 FROM INFORMATION_SCHEMA.COLUMNS

3 WHERE TABLE_NAME='Books'

Here is the result set:


In the output, you can see the newly added ISBN column.

Modifying an existing column

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

2 ALTER TABLE Books

3 ALTER COLUMN ISBN VARCHAR(50);

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:

1 DROP DATABASE LibraryDB

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:

1 DROP TABLE Books

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:

1 ALTER TABLE Books

2 DROP COLUMN ISBN

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:

1 INSERT INTO Books

2 VALUES ('Book-A', 100),

3 ('Book-B', 200),

4 ('Book-C', 150)

Let’s see if the records have been actually inserted:

1 SELECT * FROM Books

Here is the result set:


You can see the three records that we inserted in the Books table.

The TRUNCATE command will remove all the records from the Books table as shown below:

1 TRUNCATE TABLE Books

If you again select all the records from the Books table, you will see that the table is empty.

You might also like