×

SQL Data Definition Language

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.

Related Topics

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.