0% found this document useful (0 votes)
26 views28 pages

Data Definition Language DDL: Saturday, May 09, 2020 Hebansour2020

DDL commands are used to define and modify database schema. Examples of DDL commands include CREATE to create database objects like tables, ALTER to modify tables by adding or dropping columns, RENAME to change table or column names, DROP to delete tables, and TRUNCATE to remove all rows from tables while preserving the table structure. The CREATE TABLE statement is used to define the structure of a table by specifying column names, data types, and sizes.

Uploaded by

Hassan K Rayyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views28 pages

Data Definition Language DDL: Saturday, May 09, 2020 Hebansour2020

DDL commands are used to define and modify database schema. Examples of DDL commands include CREATE to create database objects like tables, ALTER to modify tables by adding or dropping columns, RENAME to change table or column names, DROP to delete tables, and TRUNCATE to remove all rows from tables while preserving the table structure. The CREATE TABLE statement is used to define the structure of a table by specifying column names, data types, and sizes.

Uploaded by

Hassan K Rayyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Lab 6

Data Definition Language


DDL

Saturday, May 09, 2020 HebaNsour2020


Saturday, May 09, 2020 HebaNsour2020
Data Definition Language
DDL

• DDL(Data Definition Language) : DDL or Data Definition Language


actually consists of the SQL commands that can be used to define the
database schema. It simply deals with descriptions of the database
schema and is used to create and modify the structure of database
objects in the database.

Saturday, May 09, 2020 HebaNsour2020


Examples of DDL commands:

• Create – is used to create the database or its objects (like table, index,
function, views, store procedure and triggers).
• Drop – is used to delete objects from the database.
• Alter-is used to alter the structure of the database.
• Truncate–is used to remove all records from a table, including all spaces
allocated for the records are removed.
• Comment –is used to add comments to the data dictionary.
• Rename –is used to rename an object existing in the database.

Saturday, May 09, 2020 HebaNsour2020


Create table

• The CREATE TABLE statement is used to create a table in SQL. We


know that a table comprises of rows and columns. So while creating
tables we have to provide all the information to SQL about the names
of the columns, type of data to be stored in columns, size of the data
etc. Let us now dive into details on how to use CREATE TABLE
statement to create tables in SQL.

Saturday, May 09, 2020 HebaNsour2020


Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
------------------------------------------------------------------------------------------------------------------
table_name: name of the table.
column1 name of the first column.
data_type: Type of data we want to store in the particular column.
For example,int for integer data.
size: Size of the data we can store in a particular column. For example if for
a column we specify the data_type as int and size as 10 then this column can store an integer
number of maximum 10 digits.

Saturday, May 09, 2020 HebaNsour2020


Data Types
Data Type Description
VARCHAR2(size) Variable-length character data
CHAR(size) Fixed-length character data
NUMBER(p,s) Variable-length numeric data
DATE Date and time values
LONG Variable-length character data (up to 2 GB)
CLOB Character data (up to 4 GB)
RAW and LONG Raw binary data
RAW
BLOB Binary data (up to 4 GB)
BFILE Binary data stored in an external file (up to 4 GB)
ROWID A base-64 number system representing the unique
address of a row in its table
Example Query:
CREATE TABLE Persons (
PersonID number (3),
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Saturday, May 09, 2020 HebaNsour2020


Example 2
CREATE TABLE Students
(
ROLL_NO number(3),
NAME varchar(20),
SUBJECT varchar(20),
);

Saturday, May 09, 2020 HebaNsour2020


Explanation
• This query will create a table named Students. The ROLL_NO field is of
type int and can store an integer number of size 3. The next two
columns NAME and SUBJECT are of type varchar and can store
characters and the size 20 specifies that these two fields can hold
maximum of 20 characters.

Saturday, May 09, 2020 HebaNsour2020


SQL | ALTER (ADD, DROP, MODIFY)

• ALTER TABLE is used to add, delete/drop or modify columns in the


existing table. It is also used to add and drop various constraints on
the existing table.

Saturday, May 09, 2020 HebaNsour2020


ALTER TABLE – ADD

• ADD is used to add columns into the existing table. Sometimes we


may require to add additional information, in that case we do not
require to create the whole database again, ADD comes to our
rescue.

Saturday, May 09, 2020 HebaNsour2020


Syntax:

ALTER TABLE table_name


ADD (
Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype
);

Saturday, May 09, 2020 HebaNsour2020


ALTER TABLE – DROP

• DROP COLUMN is used to drop column in a table. Deleting the


unwanted columns from the table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Saturday, May 09, 2020 HebaNsour2020


ALTER TABLE-MODIFY

• It is used to modify the existing columns in a table. Multiple columns


can also be modified at once.

Syntax:
ALTER TABLE table_name
MODIFY column_name column_type;

Saturday, May 09, 2020 HebaNsour2020


Sample Table:

 To ADD 2 columns AGE and COURSE to table Student.


 MODIFY column COURSE in table Student
 DROP column COURSE in table Student.

Saturday, May 09, 2020 HebaNsour2020


Output

Saturday, May 09, 2020 HebaNsour2020


SQL | ALTER (RENAME)

• Sometimes we may want to rename our table to give it a more


relevant name. For this purpose we can use ALTER TABLE to rename
the name of table.

• Syntax
ALTER TABLE table_name
RENAME TO new_table_name;

Saturday, May 09, 2020 HebaNsour2020


SQL | ALTER (RENAME)

• Columns can be also be given new name with the use of ALTER TABLE.

ALTER TABLE table_name


RENAME COLUMN old_name TO new_name;

Saturday, May 09, 2020 HebaNsour2020


QUERY:
Change the name of column NAME to FIRST_NAME in
table Student.

Saturday, May 09, 2020 HebaNsour2020


Change the name of the table Student to Student_Details

Saturday, May 09, 2020 HebaNsour2020


Drop

DROP is used to delete a whole database or just a table.


The DROP statement destroys the objects like an existing database,
table, index, or view.
A DROP statement in SQL removes a component from a relational
database management system (RDBMS).

Saturday, May 09, 2020 HebaNsour2020


Syntax:

DROP object object_name

Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.

Saturday, May 09, 2020 HebaNsour2020


Truncate
• TRUNCATE statement is a Data Definition Language (DDL) operation
that is used to mark the extents of a table for deallocation (empty for
reuse). The result of this operation quickly removes all data from a
table, typically by passing a number of integrity enforcing
mechanisms.

Saturday, May 09, 2020 HebaNsour2020


Syntax:

TRUNCATE TABLE table_name;


table_name: Name of the table to be truncated.

Saturday, May 09, 2020 HebaNsour2020


DROP vs TRUNCATE

• Truncate is normally ultra-fast and its ideal for deleting data from a
temporary table.
• Truncate preserves the structure of the table for future use, unlike
drop table where the table is deleted with its full structure.
• Table or Database deletion using DROP statement cannot be
rolled back, so it must be used wisely.

Saturday, May 09, 2020 HebaNsour2020


Saturday, May 09, 2020 HebaNsour2020
To truncate Student_details table from student_data
database.

Truncate table student_details;

• After running the above query Student_details table will be truncated,


i.e, the data will be deleted but the structure will remain in the
memory for further operations.

Saturday, May 09, 2020 HebaNsour2020

You might also like