Lecture 04 - SQL Data Definition Language
Lecture 04 - SQL Data Definition Language
Query
Language
(SQL)
With
Microsoft
SQL Server
Work Hard!!!
Study Hard!!!!
Target A+ for each Subject!!!!
Become a Champion and Make yourself
Proud!!!!
Database Management System (DBMS)
A DBMS is a set of software programs that can be used to create and maintain a database.
E.g.:
• MS Access
• MySQL
• SQL Server
• Oracle
• IBM DB2
• Mongo DB
• H2
Structured Query Language (SQL)
• It is a Keyword-based language.
Exercise
Create A Database Called Company.
Answer :
Create Database Company;
2. SELECT DATABASE
USE Database Name;
Exercise:
Answer:
Use Company;
3. CREATE TABLE
COMMON DATA TYPES
INT(n)
n = maximum number of digits
VARCHAR (n)
n= maximum length of character strings
CHAR (n)
n= maximum length of character strings.
DECIMAL (n, d)
n = maximum number of digits
d = maximum number of digits right of the decimal
DATE
The standard format is YY-Mon-DD
Syntax:
(client_no varchar(6),
name varchar(20),
address varchar(30),
telephone int,
balance_due int,
order_date date,
Primary key(client_no));
Since we need to do table constraints, let me do the Insert operation first.
Remember this is a Data Manipulation Operation.
Syntax:
INSERT INTO table_name
(column_name, column_name….)
VALUES (expression, expression…);
Exercise
INSERT FOLLOWING DATA.
Client_no Name Address Telephone Balance_due Order Date
C100 Amali Colombo 0772569014 5000 2012-05-11
Second
Method. But INSERT INTO client
only
possible if VALUES (‘C100’, ‘Amali’, ‘Colombo’, 0772569014, 5000,
data is
available for ‘2012-05-11’);
each column
Ex:
Create table SalesPerson (EMP_ID int primary key, EmpName varchar(20) not null,
Ranking int default 1 not null check (Ranking in (1,2,3)), salary decimal(7,2) not null
check(salary>=10000));
Exercise
Field Name Filed Filed
Type Size
Primary Key
Client_No Varchar 6
Cannot be Null
Name Varchar 20
Should only contains A, Address Varchar 30
B, C
Rank Varchar 1
Should between 5000 to Balance_due Decimal 8,2
50000
Order_date Date
Default value should be
System Date
Answer
• The syntax of the CREATE TABLE command with the FOREIGN KEY column
constraint is. Other than this syntax there are different ways to create a Foreign
Key on another table.
First create the Department table. Then create Employee table with a foreign key by taking
primary key of Department table.
Create table Employee (EmpNo varchar(4) primary key, Name varchar(10), Salary int,
DeptNo varchar(5) References Department (DeptNo));
Another Way
Create table Employee (EmpNo varchar(4) primary key, Name varchar(10), Salary int,
DeptNo varchar(5) Foreign key References Department (DeptNo));
Another Way
Create table Employee (EmpNo varchar(4) primary key, Name varchar(10), Salary int,
DeptNo varchar(5), Foreign key (DeptNo) References Department (DeptNo));
Example :
Invoice Order
PK PK FK FK FK
InvNo Name Date Order_Id InvNo Emp_Id Cus_Id SalesDate
SalesPerson Customer
PK PK
Emp_Id Emp_Name Cus_Id CusName Status
Create table Order (Order_Id int primary key, InvNo varchar(5), Emp_Id varchar(5),
Cus_Id varchar(5), Sales_date Date, Foreign Key (InvNo) References Invoice (InvNo),
Foreign Key (Emp_Id) References SalesPerson (Emp_Id), Foreign Key (Cus_Id)
References Customer (Cus_Id));
Composite Primary Key
If there are more than one primary key in the table you can set composite primary
keys.
PK FK PK
EmpNo Name Salary DeptNo DeptNo DepName
Example :
Suppose EmpNo and DeptNo are primary keys of the Employee table.
Create table Employee (EmpNo varchar(4), Name varchar(10), Salary int, DeptNo
varchar(5) Foreign key References Department (DeptNo), Primary Key (EmpNo,
DeptNo));
5. ALTER TABLE
ADD FIELD
Syntax
ALTER TABLE table_name
ADD newcolumn_name data_type(size),….;
Example:
Syntax
ALTER TABLE table_name
ALTER COLUMN column_name newdata_type(size),….;
Exercise
Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
Exercise
To create a FOREIGN KEY constraint on the “DeptNo" column when the “Employee" table is already
created, use the following command. But remember before you execute this command first you should
add DeptNo column to the Employee table.
Example:
ALTER TABLE Employee
ADD FOREIGN KEY (DeptNo) REFERENCES Department (DeptNo);
Another Way
ALTER TABLE Employee
ADD CONSTRAINT FK_Employee_Dept
FOREIGN KEY (DeptNo) REFERENCES Department (DeptNo);
5. ALTER TABLE
Drop the Foreign Key Constraint
Syntax:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name
Example:
ALTER TABLE Employee
DROP CONSTRAINT FK_Employee_Dept
6. Dropping Table
Syntax
Drop Table table_name
Example:
Drop table Client;
7. Truncate Table
Syntax
Truncate Table table_name
• This statement will remove all the data from the table
but table structure will remain as it is.
Example:
Truncate Table Client;
8. Rename Table
SQL Server does not have any statement that directly renames a
table. However, it does provide you with a stored procedure
named sp_rename that allows you to change the name of a
table.
Syntax:
EXEC sp_rename ‘old_table_name', ‘new_table_name'
Example:
EXEC sp_rename 'Client', 'Client_Master'
9. Describe Table
The simplest way to use a stored procedure sp_columns to show
the columns and related information about a SQL Server table is to
execute the stored procedure passing it the table name
Syntax:
Exec sp_columns table_name;
Example:
Exec sp_columns Client;
10. Drop the Database
You can drop the Database using the following syntax.
Syntax:
Drop Database database_name;
Example:
Drop Database Company;