×

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 Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 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.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 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.

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.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

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...

3 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.