×

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

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.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

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 SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 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 INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

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 GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.