0% found this document useful (0 votes)
16 views

DB Ex3

The document discusses various Data Definition Language (DDL) commands used to define and modify database structure and schema. It covers commands like CREATE, ALTER, DROP, TRUNCATE, RENAME for creating, modifying and deleting tables, columns, and databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DB Ex3

The document discusses various Data Definition Language (DDL) commands used to define and modify database structure and schema. It covers commands like CREATE, ALTER, DROP, TRUNCATE, RENAME for creating, modifying and deleting tables, columns, and databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DDL commands

Data Definition Language (DDL) or Schema Definition Language, statements are used to define
the database structure or schema.
These statements handle the design and storage of database objects.

CREATE:
CREATE DATABASE:
The CREATE DATABASE statement is used to create a new SQL database.
Syntax
CREATE DATABASE databasename;
Example
The following SQL statement creates a database called "testDB":

CREATE DATABASE testDB;

CREATE TABLE:
The CREATE TABLE statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
 The column parameters specify the names of the columns of the table.
 The datatype parameter specifies the type of data the column can hold (e.g. varchar,
integer, date, etc.).
Example
The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DESC Statement (Describe Table):
SQL DESC statement use for describe the list of column definitions for specified table. You can
use either DESC or DESCRIBE statement.
Syntax:
Describe table_name

Example:
Describe persons;

+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| PersonID | int | YES | | NULL | |
| LastName | varchar(255)| YES | | NULL | |
| Firstname| varchar(255)| YES | | NULL | |
| Address | varchar(255)| YES | | NULL | |
| City | varchar(25) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
Field indicates the column name, Type is the data type for the column, NULL indicates whether
the column can contain NULL values, Key indicates whether the column is indexed, and Default
specifies the column's default value. Extra displays special information about columns:

ALTER TABLE:
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.

ALTER TABLE - ADD Column


To add a column in a table.
Syntax:
ALTER TABLE table_name
ADD column_name datatype;

Example:
The following SQL adds an "Email" column to the "Persons" table:
ALTER TABLE Persons
ADD Email varchar(255);

Add Multiple Column in table:


ALTER TABLE table_name
ADD ( column_name1 datatype[(size)],
column_name2 datatype[(size)],
...
);

ALTER TABLE - DROP COLUMN


It is used to delete a column in a table
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Example:
The following SQL deletes the "Email" column from the "Persons" table:
ALTER TABLE Persons
DROP COLUMN Email;

MODIFY EXISTING COLUMN IN TABLE


To change the data type of a column in a table
Syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

RENAME THE EXISTING COLUMN IN A TABLE


Syntax:
ALTER table table_name change old_column_name new_column_name datatype;
Example:
ALTER table persons change city district varchar(50);
RENAME:
RENAME command is used to set a new name for any existing table.
Syntax (Rename Table):
RENAME TABLE old_table_name to new_table_name
Example:
RENAME TABLE persons to students_info;

The above query will rename the table student to students_info.

TRUNCATE:
It is used to delete all the rows from the table and free the space containing the table.
Syntax:
TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE Persons;

DROP TABLE / DATABASE


The DROP TABLE / DATABASE statement is used to drop an existing table in a database or a
database.
Syntax
DROP TABLE table_name;

Example
The following SQL statement drops the existing table "Person":
DROP TABLE Person;

You might also like