0% found this document useful (0 votes)
11 views

Lab Manual Week 07

The document provides an overview of important DDL commands in SQL including commands to create, alter, and drop databases and tables. It also covers creating indexes and backing up databases. The learning objective is understanding DDL commands and the document provides examples of syntax for key DDL statements.

Uploaded by

waliaslam2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab Manual Week 07

The document provides an overview of important DDL commands in SQL including commands to create, alter, and drop databases and tables. It also covers creating indexes and backing up databases. The learning objective is understanding DDL commands and the document provides examples of syntax for key DDL statements.

Uploaded by

waliaslam2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 262L- Database Systems

Lab Manual 8

Instructor:
• Mr. Nazeef Ul Haq and Mr. Nauman Babar
Learning Objectives:
• Understanding of DDL commands.

Helping Material:
1. Important DDL Statements
The most important DDL statements in SQL are following:

• CREATE DATABASE - creates a new database


CREATE DATABASE databasename;
• ALTER DATABASE - modifies a database
DROP DATABASE databasename;
• CREATE TABLE - creates a new table
CREATE TABLE table_name (
column1 datatype NOT NULL/ IDENTITY(1,1)/ UNIQUE/ Primary Key,
column2 datatype,
column3 datatype CHECK (condition),
PRIMARY KEY (column_name), (You can also make primary key in this way)
FOREIGN KEY (column_name) REFERENCES Table(column_name) (You can make foreign key in
this way)
....
);
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;
OR
SElECT * Into <DestinationTableName> FROM <SourceTableName> WHERE condition;
• ALTER TABLE - modifies a table
ALTER TABLE table_name
ADD column_name datatype;
DROP COLUMN column_name;
ALTER COLUMN column_name datatype;
ADD CONSTRAINT PK_Person PRIMARY KEY (Column1,Column2)
ADD Column_name datatype FOREIGN KEY REFERENCES Table_name(column_name)
ADD CONSTRAINT df_City DEFAULT 'Sandnes' FOR City;
ALTER COLUMN City DROP DEFAULT;
• DROP TABLE - deletes a table
DROP TABLE table_name;
TRUNCATE TABLE table_name;
• CREATE INDEX - creates an index (search key)
CREATE INDEX/UNIQUE INDEX index_name ON table_name (column1, column2, ...);
CREATE INDEX idx_pname ON Table_name (LastName, FirstName);
• DROP INDEX - deletes an index
DROP INDEX table_name.index_name;

Page 1 of 2
• BackUp Database
BACKUP DATABASE databasename TO DISK = 'filepath\file_name.bak';
BACKUP DATABASE databasename TO DISK = 'filepath' WITH DIFFERENTIAL;
What to Submit:
• Nothing ☺

Page 2 of 2

You might also like