Lab 02 - DDL
Lab 02 - DDL
Example:
Create database myDB;
2. Use a database:
To tell the DBMS about the database in which we want to create database tables or add data, we
need to use the following syntax after creating our database:
Use databasename;
3. Delete a Database:
We can delete a database by using the following syntax:
Drop database databasename
Example:
Drop database myDB;
The above statement will delete all tables of myDB and their data.
Example:
Create Table Student
(
rollNumber int,
name nvarchar(100),
cnic nvarchar(100),
cgpa float
)
To learn about datatypes in SQL Server visit the following link, and see datatypes under SQL SERVER
Data types:
https://www.w3schools.com/sql/sql_datatypes.asp
5. Drop a Table:
A table can be drop using the following syntax: (Dropping a table will delete the table data as well as
table definition)
Drop Table tableName
Example:
Drop Table Student
6. Truncate a Table:
A table can be truncated using the following syntax: (Truncating a table will only delete the data in
the table and not table definition)
Truncate Table tableName
Example:
Truncate Table Student
7. SQL Constraints
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
Constraints can be specified when the table is created with the CREATE TABLE statement, or after
the table is created with the ALTER TABLE statement.
In this case, the name of the old and new column names are the same except that the column must
have a NOT NULL constraint:
Alter Table Student
CHANGE
rollNumber
rollNumber int NOT NULL;
b. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
(OR)
(OR)
(OR)
If we want to update the foreign key value when the corresponding primary key value is updated,
and delete the foreign key value whenever the corresponding primary key value is deleted; we use
cascade option.
If we want to set the foreign key value to null whenever the corresponding primary key value is
updated or deleted, then we use set NULL option.
If we want to force the foreign key to remain unchanged, we will use No Action option. If No Action
is specified, then the primary key value will not be allowed to delete or update in the referenced
table if there is a corresponding value of foreign key in the referencing table.
Example:
Create Table Student
(
rollNumber int primary key,
deptId int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
)
alter table Student add constraint FK_STUDENT foreign key (deptId) references Department
(departmentId) on delete No Action on update Cascade
e. Check Constraint
The check constraint is used to limit the value range that can be placed in a column.
Two types of constraints:
1. MySQL check constraints –column exmaple
(OR)
Create Table Student
(
rollNumber int,
name nvarchar(50) NOT NULL,
cnic nvarchar(50) NOT NULL,
cgpa float NULL,
check (cgpa>=0 AND cgpa<=4)
)
Use following command to view the table definition
Check Constraint can be applied to relate the value of a column with another column:
Create Table StudentMarks
(
rollNumber int,
obtainedMarks float,
maximumMarks float,
check (obtainedMarks >=0 AND obtainedMarks<= maximumMarks)
)
f. Default Constraint
Default constraint is used to assign a default value to a column when no value is specified by the
user during data insertion.
g. Drop a Constraint
alter table Student drop constraint DEFAULT_SUDENT_CGPA
MySQL Syntax
Example
Rename Column:
Syntax:
Example:
END