SQL Data Definition Language

tutorial and example
tutorial and example

Data definition language directly effects on the structure/schema of the database.

CREATE, ALTER, DROP are the commands of DDL.

CREATE: Creates new database, table, or view of table.

ALTER: Modifies the database or table.

MODIFY, ADD keywords are used in altering data like altering table schema or altering database.

If user remember the things after creation of table at that time alter command used for modification.

DROP: Deletes entire table with its structure/schema or deletes view of table or other objects of database.

Syntax:

CREATE DATABASE Database_name;

Example:

Create Database College;

Syntax:

CREATE TABLE table_name

(column_name1 data type, column_name2 data type,………);

Example:

CREATE TABLE STUDENT (student_id int,

student_name varchar(50),

student_dept varchar(50),

classyear varchar(10));

See the output screen carefully, screen prompting MariaDB [test] >, here MariaDB is the database System and test is the name of database in which we are performing SQL operations.

  • DESC: The DESC command shows the structure/ schema of the table. Schema means its structure. In a simple language, ‘it is a blueprint of a table’, which shows table name with its details like how many columns are present in that table, data type of each column means which type of value a column can hold. It shows keys, it shows whether column can hold null value or not, default values and extra information.

Syntax:

DESC TABLE_NAME;

Example:

Desc student;

# Syntax for Creating View

Syntax:

CREATE VIEW view_name AS

SELECT column 1,column 2,…..

FROM Table_name

[WHERE condition];

Example:

Create view student_view  as

Select * from student

Where Dept = ’IT’;

Syntax:

Alter Table

Table_name

Add column column_name data type;

Example:

Alter Table

Student

Add Column City Varchar(10);

New column ‘city’ is added to the STUDENT table. DESC command shows structure of student table before alteration and after alteration of table. Yet, we have not entered any value to the new column ‘city’. NULL is a default value for each column.

City column shows null value for all records present in the table.

Syntax:

Alter Table

Table_name

Modify column  column_name data type constraint;

Example:

Alter Table

Student

Modify Column student_id int NOT NULL;

Schema level modification is done using alter table. Not null constraint is added on column student_id.

Syntax:

Alter Table Table_name

Add constraint_name (column_name);

Example:

Alter Table

Student

Add Primary Key(student_id,student_name);

Primary key is added on two columns means combination of student_id and student_name become a primary key for table student.

Syntax:

Alter Table

Table_name

DROP column  column_name;

Example:

Alter Table

Student

DROP Column city;

Here ‘city’ column is deleted using alter keyword. Now city column is not present in student table.

Output screen shows the structure of the table before deleting column city and also structure after deletion of city column.

Syntax:

DROP

Table table_name;

Example:

DROP

Table Student;

Drop command deletes all the data i.e. all records with structure/schema of the table from database.