0% found this document useful (0 votes)
3 views16 pages

Lab 04 - SQL (DDL)

Data Definition Language

Uploaded by

hayamousa78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Lab 04 - SQL (DDL)

Data Definition Language

Uploaded by

hayamousa78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

SQL PRIMARY KEY Constraint

• The PRIMARY KEY constraint uniquely identifies each record in a


database table.

• Primary keys must contain UNIQUE values, and cannot contain NULL
values.

• A table can have only one primary key, which may consist of single or
multiple fields.

– CREATE TABLE Persons (


ID number(9,0) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number (3,0)
);
SQL PRIMARY KEY Constraint [Con.]
– CREATE TABLE Persons (
ID number(9,0) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number(3,0),
CONSTRAINT PK_Person PRIMARY KEY (ID)
);

– CREATE TABLE Persons (


ID number(9,0) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number(3,0),
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
Alter / Primary Key
– ALTER TABLE Persons ADD PRIMARY KEY (ID);

– ALTER TABLE Persons


ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
;

– ALTER TABLE Persons DROP CONSTRAINT PK_Person;


SQL FOREIGN KEY Constraint
• A FOREIGN KEY is a key used to link two tables together.

• A FOREIGN KEY is a field (or collection of fields) in one table that


refers to the PRIMARY KEY in another table.

• The table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table.

• CREATE TABLE Orders (


OrderID number(5,0) NOT NULL PRIMARY KEY,
OrderNumber number(5,0) NOT NULL,
PersonID number(9,0) FOREIGN KEY REFERENCES Persons(PersonID)
);
SQL FOREIGN KEY Constraint [Con]
• CREATE TABLE Orders (
OrderID number(5,0) NOT NULL,
OrderNumber number(5,0) NOT NULL,
PersonID number(9,0),
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
Alter / FOREIGN Key
• ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

• ALTER TABLE Orders


ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

• ALTER TABLE Orders


DROP CONSTRAINT FK_PersonOrder;
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.

• CREATE TABLE Persons (


ID number(9,0) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number(3,0) CHECK (Age>=18)
);
SQL CHECK Constraint [Con.]
• CREATE TABLE Persons (
ID number(9,0) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age number(3,0),
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);

• ALTER TABLE Persons ADD CHECK (Age>=18);

• ALTER TABLE Persons


ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes
');

• ALTER TABLE Persons


DROP CONSTRAINT CHK_PersonAge;
SQL DEFAULT Constraint
• The DEFAULT constraint is used to provide a default
value for a column.

• The default value will be added to all new records


IF no other value is specified.

• CREATE TABLE Persons (


ID number(9,0) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
City varchar(255) DEFAULT 'Sandnes'
);
SQL DEFAULT Constraint [Con.]
• ALTER TABLE Persons
MODIFY City DEFAULT 'Sandnes';

• ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT;
Views
Views
• SQL allows a “virtual relation” to be defined by a query,
and the relation conceptually contains the result of the
query.

• The virtual relation is not precomputed and stored, but


instead is computed by executing the query whenever the
virtual relation is used.

• Any such relation that is not part of the logical model, but
is made visible to a user as a virtual relation, is called a
view.

• It is possible to support a large number of views on top of


any given set of actual relations.
View Definition
• create view v as <query expression>;

• Ex:
create view faculty as
select ID, name, dept_name
from instructor;
Using Views in SQL Queries
create view faculty as
select ID, name, dept_name
from instructor;

• Select name from faculty;

• Select count(ID) from faculty where


dept_name =“CSE”;

• Select count(ID), dept_name from


faculty group by (dept_name);
Materialized Views
• Certain database systems allow view relations
to be stored, but they make sure that, if the
actual relations used in the view definition
change, the view is kept up-to-date. Such
views are called materialized views.
• In general, an SQL view is said to be updatable (that is,
inserts, updates or deletes can be applied on the view) if the
following conditions are all satisfied by the query defining the
view:
– The from clause has only one database relation.
– The select clause contains only attribute names of
the relation, and does not have any expressions,
aggregates, or distinct specification.
– Any attribute not listed in the select clause can be
set to null; that is, it does not have a not null
constraint and is not part of a primary key.
– The query does not have a group by or having
clause.

You might also like