Introduction

Most database users need not worry about creating a database; they use interactive SQL to access a database of corporate information or access a database that has been created by someone else. In a typical corporate database, for example, the database administrator may give you permission to retrieve and perhaps update the stored data. However, the administrator will not allow you to create new databases or modify the structure of the existing tables.

Unfortunately for you, you will also be expected to know how to create databases and define their structure!

The SELECT, INSERT, DELETE, UPDATE, COMMIT, and ROLLBACK statements are all concerned with manipulating the data in a database. These DML statements can modify the data stored in the database, but they cannot change its structure. None of these statements create or delete tables or columns for example. Changes to the structure of the database are handled by a different set of SQL statements, usually called the SQL Data Definition Language or DDL.

Using DDL statements it is possible to

The Data Definition Language is based on three SQL verbs:

CREATE TABLE Statement

The CREATE TABLE statement defines a new table in the database and prepares it to accept data. The various clauses of the statement specify the elements of the table definition.

Syntax

Create table Table_name (column_ name (Data_type));

Note. Do not attempt to "learn from" these diagrams. This is simply provided as a sample for you to see the syntax.

The columns of the newly created table are defined in the body of the CREATE TABLE statement. The column definitions appear in a comma-separated list enclosed in parentheses. The order of the column definitions determines the left-to-right order of the columns in the table.

In the preceding syntax

Example

CREATE TABLE EMPLOYEE(ID INTEGER NOT NULL UNIQUE,SURNAME VARCHAR(30) NOT NULL,FIRSTNAME VARCHAR(30) NOT NULL,EMAIL VARCHAR(80),
        COUNTRY VARCHAR(30) DEFAULT 'India',PHONE VARCHAR(25)) 

As seen in the first CREATE TABLE example above, it is possible to define "default" values for columns. If a column has a default value, it is specified within the column definition. Even if a column is defined as NOT NULL but has a default set, its omission in an INSERT statement would not cause an issue, since it would simply default to the chosen value.

In addition to defining the columns of a table, the CREATE TABLE statement identifies the table's primary key and the table's relationships to other tables in the database. The PRIMARY KEY and FOREIGN KEY clauses handle these functions. The PRIMARY KEY clause specifies the column or columns that form the primary key for the table. The FOREIGN KEY clause specifies a foreign key in the table and the relationship that it creates to another (parent) table in the database. The FOREIGN KEY clause specifies:

DROP TABLE in SQL

Sometimes it is necessary to dispose of old tables, which are no longer in use. This is done through the use of the DROP TABLE statement. The table name in the statement identifies the table to be dropped. Normally you will be dropping one of your own tables and will use an unqualified table name, but it is possible to drop a table owned by another user, assuming that you have the required permission.

The syntax for the DROP table statement can be seen below:

Drop table Tbale_name  

Example

Drop table  Employee

ALTER TABLE in SQL

After a table has been in use for some time, users often discover that they want to store additional information about the entities represented in the table. Changes to table structures can be handled using the ALTER TABLE statement.

Using the ALTER TABLE statement we can

There are a number of issues that may occur when performing ALTER TABLE statements. First, when adding a new column to an existing table, the DBMS assumes a NULL value for all existing rows in the table. If the column is declared to be NOT NULL with a default value then the DBMS assumes the default value. It is not possible to simply declare the new column NOT NULL, because the DBMS would be immediately violating this constraint!

Dropping a column can pose similar problems as those encountered in deletes and updates. For example, if you drop a column that is a primary key in some relationship, the foreign key columns that refer to the dropped column become invalid.

The SQL2 standard deals with these issues in the same way that it handled the potential data integrity problems posed by DELETE and UPDATE statements - with a drop rule that operates in a similar way to the delete and update rules. You can specify one of two drop rules; RESTRICT (causing an error if dependencies exist) and CASCADE (any other dependency objects are also dropped).

Summary

This article taught us about DDL statements used in SQL Server. I hope this article is useful for all readers. If you have any suggestions then please contact me.