0% found this document useful (0 votes)
16 views16 pages

Lab 03 - SQL (DDL)

Data Definition Language

Uploaded by

hayamousa78
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)
16 views16 pages

Lab 03 - SQL (DDL)

Data Definition Language

Uploaded by

hayamousa78
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/ 16

SQL

• SQL is a standard language for storing,


manipulating and retrieving data in databases.
DDL
• DDL is abbreviation of Data Definition
Language.
– It is used to create and modify the structure of
database objects in database.
• CREATE – Creates objects in the database
• ALTER – Alters objects of the database
• DROP – Deletes objects of the database
• TRUNCATE – Deletes all records from a table and resets
table identity to initial value.
DML
• DML is abbreviation of Data Manipulation
Language.
– It is used to retrieve, store, modify, delete, insert
and update data in database.
• SELECT – Retrieves data from a table
• INSERT – Inserts data into a table
• UPDATE – Updates existing data into a table
• DELETE – Deletes all records from a table
DCL
• DCL is abbreviation of Data Control Language.
– It is used to create roles, permissions, and
referential integrity as well it is used to control
access to database by securing it.
• GRANT – Gives user’s access privileges to database
• REVOKE – Withdraws user’s access privileges to
database given with the GRANT command
TCL
• TCL is abbreviation of Transactional Control
Language.
– It is used to manage different transactions
occurring within a database.
• COMMIT – Saves work done in transactions
• ROLLBACK – Restores database to original state since
the last COMMIT command in transactions
SQL CREATE TABLE Statement
• The CREATE TABLE statement is used to create a
new table in a database.

• Simple Example:
CREATE TABLE Persons (
PersonID number(9,0),
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
SQL DROP TABLE Statement
• The DROP TABLE statement is used to drop an
existing table in a database.

– DROP TABLE table_name;


SQL TRUNCATE TABLE
• The TRUNCATE TABLE statement is used to
delete the data inside a table, but not the
table itself.
– TRUNCATE TABLE table_name;
SQL ALTER TABLE Statement
• 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 / Change Datatype
Alter / Drop Columns
– ALTER TABLE table_name DROP COLUMN
column_name;

– ALTER TABLE table_name DROP


(column_name1, column_name2);

– Ex
• ALTER TABLE customers DROP
customer_name;
Alter / Add Column
– ALTER TABLE table_name ADD
column_name column_definition;

– ALTER TABLE table_name ADD


(column_name1 column1_definition,
column_name2 column2_definition);
– Ex:
• ALTER TABLE customers ADD
customer_name varchar2(45);
Alter / Rename tables and columns
• ALTER TABLE table_name RENAME TO
new_table_name;

• ALTER TABLE table_name RENAME COLUMN


column_name TO new_column_name;
SQL Constraints
• NOT NULL :
– Ensures that a column cannot have a NULL value

• UNIQUE :
– Ensures that all values in a column are different

• PRIMARY KEY :
– A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table

• FOREIGN KEY :
– Uniquely identifies a row/record in another table

• CHECK :
– Ensures that all values in a column satisfies a specific condition

• DEFAULT :
– Sets a default value for a column when no value is specified

• INDEX :
– Used to create and retrieve data from the database very quickly
NOT NULL
• By default, a column can hold NULL values.

• The NOT NULL constraint enforces a column to NOT accept NULL values.

• This enforces a field to always contain a value, which means that you
cannot insert a new record, or update a record without adding a value
to this field.

– CREATE TABLE Persons (


ID number(9,0) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age number(3,0)
);
SQL UNIQUE Constraint
• The UNIQUE constraint ensures that all values in a column are different.

• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.

• A PRIMARY KEY constraint automatically has a UNIQUE constraint.

• However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.

– CREATE TABLE Persons (


ID number(9,0) NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

You might also like