Lab # 05 DDL
Lab # 05 DDL
Lab # 05 DDL
LAB # 05
Lab Objective:
Syntax
CREATE DATABASE databasename;
Example
CREATE DATABASE testDB;
Syntax
DROP DATABASE databasename;
Example
DROP DATABASE testDB;
Syntax
CREATE TABLE table_name (
column1 datatype,
Database System [LAB] Department of CS & IT
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
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The LastName, FirstName, Address, and City columns are of type varchar and
will hold characters, and the maximum length for these fields is 255 characters.
Syntax
DROP TABLE table_name;
Database System [LAB] Department of CS & IT
Example
DROP TABLE Shippers;
Syntax
TRUNCATE TABLE table_name;
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
ALTER TABLE table_name
ADD column_name datatype;
Example
ALTER TABLE Customers
ADD Email varchar(255);
ALTER TABLE table_name
DROP COLUMN column_name;
The following SQL deletes the "Email" column from the "Customers" table:
Example
ALTER TABLE Customers
DROP COLUMN Email;
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;
ALTER TABLE Persons
DROP COLUMN DateOfBirth;
Database System [LAB] Department of CS & IT
SQL Constraints
SQL constraints are used to specify rules for data in a table.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any
violation between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to
a column, and table level constraints apply to the whole table.