Chapter 6 The SQL-DDL Language

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

Chapter 5 The SQL Language

The name SQL stands for Structured Query Language. It is


pronounced “S-Q-L” and can also be pronounced “sequel.”
SQL is a standard language for accessing and manipulating
databases.
SQL is a computer language designed to get information
from data that is stored in a relational database.
SQL is different from most other computer languages. With
SQL, you describe the type of information you want. The
computer then determines the best procedure to use to
obtain it and runs that procedure. This is called a
declarative computer language because the focus is on the
result
• You specify what the result should look like. The computer is
allowed to use any method of processing as long as it obtains
the correct result.
• Most other computer languages are procedural. These are
languages like C, Cobol, Java, Assembler, Fortran, Visual Basic,
and others. In these languages, you describe the procedure that
will be applied to the data; you do not describe the result. The
result is whatever emerges from applying the procedure to the
data.
• SQL is a nonprocedural language, in contrast to the procedural
or third generation languages (3GLs) such as COBOL and C that
had been created up to that time.
• SQL describes what data to retrieve, delete, or insert, rather
than how to perform the operation.
• Two standards organizations, the American National Standards
Institute (ANSI) and the International Standards Organization
(ISO), currently promote SQL standards to industry.
• What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
• SQL DML and DDL
• SQL can be divided into two parts: The Data
Manipulation Language (DML) and the Data
Definition Language (DDL).
• The query and update commands form the DML
part of SQL:
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• The DDL part of SQL permits database tables to be created or
deleted. It also defines indexes (keys), specifies links between
tables, and imposes constraints between tables. The most
important DDL statements in SQL are:
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
The CREATE DATABASE Statement
• The syntax for the typical CREATE DATABASE
statement looks like this:
SYNTAX:
CREATE DATABASE database_name
Example: Use SQL Server to create a database
called PAYROLL
• SQL:
CREATE DATABASE university
The CREATE TABLE Statement
• Here's the basic syntax for the CREATE TABLE
statement:
CREATE TABLE table_name
(field1 datatype [ NOT NULL ],
field2 datatype [ NOT NULL ],
field3 datatype [ NOT NULL ]...)
Data Types in SQL Server
• Example: Using SQL Server Create the follwing tables
• Department(dno,dname)
• Student(idNo,fname,lname,age,sex,dnumber)

Solution:
CREATE TABLE department
(
dno int not null,
Dname varchar(15) not null
)
CREATE TABLE student
(
idNo varchar(10) not null,
fname varchar(15),
lname varchar(15),
age int ,
sex char(1),
dnumber int
)
Implementing Constraints

1. Primary Key Constraints


a) Adding a primary key when creating a table
 
CREATE TABLE dept
(
dno int IDENTITY(1,1) PRIMARY KEY,
dname varchar(50) NOT NULL
)
b) Adding a primary key after a table created
This section shows how to add a primary key to a
table, even after the table contains many rows of
data. The syntax is:
 
ALTER TABLE table_name
ADD CONSTRAINT name_of_the_constraint
PRIMARY KEY
(list_of_columns_in_the_primary_key);
Remark: In Sql Server, the column to be used as Pk
should defined as NOT NULL
Example: Changing the primary key of a table
alter table department
add constraint pk
primary key (dno);
• Dropping primary key constraint
• Example:
alter table department
drop pkconstraintName;
• Method 2: Not using the name of the
constraint to drop it
alter table dept
drop primary key;
alter table dept
add constraint pk1
primary key (dno);
2) Foreign Key Constraints
Example:
create table stud(
id int primary key,
name nvarchar(15),
dnumber int references dept(dno) on update
cascade on delete cascade
)
• OR
create table stud(
id int primary key,
name nvarchar(15),
dnumber int null foreign key(dnumber)
references dept(dno) on update cascade on
delete cascade
)
• OR
create table stud
(
id int not null ,
name nvarchar(15),
dnumber int null ,
primary key(id),
CONSTRAIN fk foreign key(dnumber) references dept(dno)
)
• SQL FOREIGN KEY Constraint on ALTER TABLE
ALTER TABLE stud
ADD FOREIGN KEY (dnumber)
REFERENCES dept(dno)
• OR
 ALTER TABLE stud
ADD CONSTRAINT fk
FOREIGN KEY (dnumber)
REFERENCES dept(dno)
• To DROP a FOREIGN KEY Constraint
• To drop a FOREIGN KEY constraint, use the
following SQL:
ALTER TABLE stud
DROP CONSTRAINT fk
• 3) Default Constraints
• SQL DEFAULT Constraint
• The DEFAULT constraint is used to insert a
default value into a column.
• The default value will be added to all new
records, if no other value is specified.
• SQL DEFAULT Constraint on CREATE TABLE
• The following SQL creates a DEFAULT constraint on the
"City" column when the "Persons" table is created:
• CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)
• SQL DEFAULT Constraint on ALTER TABLE
• To create a DEFAULT constraint on the "City"
column when the table is already created, use
the following SQL:
• SQL Server / MS Access:
• ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'SANDNES‘
• ALTER TABLE Employee ADD CONSTRAINT
DF_SomeName DEFAULT 'SANDNES' FOR City;
• To DROP a DEFAULT Constraint
• To drop a DEFAULT constraint, use the
following SQL:
• SQL Server / Oracle / MS Access:
• ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT
• Or
ALTER TABLE Persons
DROP CONSTRAINT defaultContsraintName;
• SQL CHECK Constraint
• The CHECK constraint is used to limit the value
range that can be placed in a column.
• If you define a CHECK constraint on a single
column it allows only certain values for this
column.
• If you define a CHECK constraint on a table it can
limit the values in certain columns based on
values in other columns in the row.
• SQL CHECK Constraint on CREATE TABLE
• The following SQL creates a CHECK constraint on the "P_Id" column
when the "Persons" table is created. The CHECK constraint specifies
that the column "P_Id" must only include integers greater than 0.
• SQL Server / Oracle / MS Access:
• CREATE TABLE Persons
(
P_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
• To allow naming of a CHECK constraint, and for defining a CHECK
constraint on multiple columns, use the following SQL syntax:
• MySQL / SQL Server / Oracle / MS Access:
• CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
)
• SQL CHECK Constraint on ALTER TABLE
• To create a CHECK constraint on the "P_Id"
column when the table is already created, use
the following SQL:
• MySQL / SQL Server / Oracle / MS Access:
• ALTER TABLE Persons
ADD CHECK (P_Id>0)
• To allow naming of a CHECK constraint, and for
defining a CHECK constraint on multiple
columns, use the following SQL syntax:
• MySQL / SQL Server / Oracle / MS Access:
• ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>0
AND City='Sandnes')
• To DROP a CHECK Constraint
• To drop a CHECK constraint, use the following
SQL:
• SQL Server / Oracle / MS Access:
• ALTER TABLE Persons
DROP CONSTRAINT chk_Person
• SQL CREATE INDEX Statement
• The CREATE INDEX statement is used to create
indexes in tables.
• Indexes allow the database application to find
data fast; without reading the whole table.
• Indexes
• An index can be created in a table to find data
more quickly and efficiently.
• The users cannot see the indexes, they are just
used to speed up searches/queries.
Note: You should only create indexes on
columns (and tables) that will be frequently
searched against.
• SQL CREATE INDEX Syntax
• Creates an index on a table. Duplicate values
are allowed:
• CREATE INDEX index_name
ON table_name (column_name)
• SQL CREATE UNIQUE INDEX Syntax
• Creates a unique index on a table. Duplicate
values are not allowed:
• CREATE UNIQUE INDEX index_name
ON table_name (column_name)
• Note: The syntax for creating indexes varies
amongst different databases. Therefore: Check
the syntax for creating indexes in your database.
• CREATE INDEX Example
• The SQL statement below creates an index
named "PIndex" on the "LastName" column in
the "Persons" table:
• CREATE INDEX PIndex
ON Persons (LastName)
• If you want to create an index on a
combination of columns, you can list the
column names within the parentheses,
separated by commas:
• CREATE INDEX PIndex
ON Persons (LastName, FirstName)
• SQL DROP INDEX, DROP TABLE, and DROP
DATABASE
• Indexes, tables, and databases can easily be
deleted/removed with the DROP statement.
• The DROP INDEX Statement
• The DROP INDEX statement is used to delete
an index in a table.
• DROP INDEX Syntax for MS Access:
• DROP INDEX index_name ON table_name
• DROP INDEX Syntax for MS SQL Server:
• DROP INDEX table_name.index_name
• The DROP TABLE Statement
• The DROP TABLE statement is used to delete a
table.
• DROP TABLE table_name
• The TRUNCATE TABLE Statement
• What if we only want to delete the data inside
the table, and not the table itself?
• Then, use the TRUNCATE TABLE statement:
• TRUNCATE TABLE table_name
• SQL ALTER TABLE Syntax
• To add a column in a table, use the following
syntax:
• ALTER TABLE table_name
ADD column_name datatype
• To delete a column in a table, use the
following syntax (notice that some database
systems don't allow deleting a column):
• ALTER TABLE table_name
DROP COLUMN column_name
• To change the data type of a column in a table,
use the following syntax:
• SQL Server / MS Access:
• ALTER TABLE table_name
ALTER COLUMN column_name datatype
• SQL ALTER TABLE Example
• Persons(id,fname,lname,address,city)
• Now we want to add a column named
"DateOfBirth" in the "Persons" table.
• We use the following SQL statement:
• ALTER TABLE Persons
ADD Sex char(1)
• Change Data Type Example
• Now we want to change the data type of the
column named "DateOfBirth" in the "Persons"
table.
• We use the following SQL statement:
• ALTER TABLE Persons
ALTER COLUMN Sex int
• DROP COLUMN Example
• Next, we want to delete the column named
"DateOfBirth" in the "Persons" table.
• We use the following SQL statement:
• ALTER TABLE Persons
DROP COLUMN DateOfBirth

You might also like