SQL-The Relational Database Standards
SQL-The Relational Database Standards
standards
Introduction to SQL
SQL data definition and data types
Specfying basic constraints in SQL
Schema change statements in SQL
Basic queries in SQL
More Complex SQL queries
Additional features in SQL
Views,Cursor,Triggers,and PL/SQL
programming
Introduction to SQL
Considered one of the major reasons for the commercial
success of relational databases
More user friendly than two formal languages
Structured Query Language(SQL) originally called
Structured English Query Language(SQUEL)
Designed and implemented at IBM
The standaradizaton of SQL is joint effort by ANSI
(American National Standards) and ISO(International
Standard Organization)
The first SQL standard is SQL-86 orSQL1,and later
SQL2 and SQL3 (In 1999 ) released
Introduction to SQL
SQL is comprehensive database language
Has data definitions queries, updates. Hence it is both
DDL &DML
Has the facilities for defining views on the database
by specifying security and authorization, for defining
integrity constraints and for specifying transaction
controls.
Has rules for embedding SQL statements into general
purpose programming languages such as Java or
C/C++
Introduction to SQL
SQL is comprehensive database language
Has data definitions queries, updates. Hence it is both
DDL &DML
Has the facilities for defining views on the database
by specifying security and authorization, for defining
integrity constraints and for specifying transaction
controls.
Has rules for embedding SQL statements into general
purpose programming languages such as Java or
C/C++
Introduction to SQL
SQL standards staring with SQL 1999 are divided into
1.Core specialization: Implemented by all RDBMS vendors that
are SQL complaint.
2.Specialized extensions:
Can be implemented as optional modules
To be purchased independently for specific database applications
such datamining, data warehousing etc.
Introduction to SQL
SQL standards staring with SQL 1999 are divided into
1.Core specialization: Implemented by all RDBMS vendors that
are SQL complaint.
2.Specialized extensions:
Can be implemented as optional modules
To be purchased independently for specific database applications
such datamining, data warehousing etc.
SQL data definition and data types
Terminology:
Table, row, and column used for relational model
terms relation, tuple, and attribute
CREATE statement
Main SQL command for data definition
SQL data definition and data types
Ex:bit(5)
Literal strings are placed between single quotes but preceded by B to
distinguish from character string
EX: B ‘10001’
Attribute Data Types and Domain
Constraint
• Bit string example
CREATE TABLE test (a BIT(3), b BIT VARYING(5));
EXAMPLES
ALTER TABLE STUDENT ADD CONSTRAINT PK_STUD PRIMARY KEY (USN);
Adding constraints without giving name
• Dropping constraints
ALTER TABLE DROP CONSTRAINT drops a constraint on an existing table.
EX:ALTER TABLE STUDENT DROP PK_STUD;
Specifying constraints in SQL
Constraints are the rules for the data in a table.
Constraints Can be specified when the table is
created or after the table is created
Constraints are classified as follows
1. Specifying attribute constraint and attribute
defaults
2. Specifying key and referential integrity
constraints
3. Giving name to constraint
4. Specifying constraints on tuples using CHECK
Specifying constriants in SQL
1. Specifying attribute constraint and attribute
defaults
NOT NULL Constraint
• By default, a column can hold NULL values.
• The NOT NULL constraint enforces a column to NOT
accept NULL values.
• This enforces a field to always contain a value,
which means that you cannot insert a new record,
or update a record without adding a value to this
field.
Specifying constriants in SQL
1. Specifying attribute constraint and attribute
defaults
NOT NULL Constraint
ON create table
• CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL,USN
CHAR(8) NOT NULL,SEM INT);
CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL,USN CHAR(8) ,SEM INT DEFAULT 1);
ON existing table
ALTER TABLE STUDENT ADD SEM INT DEFAULT 1; //if column sem not exist in table
or
2) CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL,USN CHAR(8) ,SEM INT);
ALTER TABLE STUDENT MODIFY SEM DEFAULT 1; //if column exist in table
ALTER TABLE STUDENT ADD CONSTRAINT DEF_SEM SEM INT DEFAULT 1 //Giving constrint name
Specifying constraints in SQL
1. Specifying attribute constraint and attribute defaults
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 column it will
allow 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.
Specifying constraints in SQL
1. Specifying attribute constraint and attribute defaults
• CHECK CONSTRAINT
• ON create table
1) CREATE TABLE STUDENT(NAME VARCHAR(20),USN CHAR(9),SEM INT CHECK (SEM>=1
AND SEM<=8) );
• ON existing table
UNIQUE KEY
PRIMARY KEY CONSTRAINT
REFERENTIAL INTEGRITY CONSTRAINT
Specifying constraints in SQL
2)Specifying key and referential integrity constraint
contd..
UNIQUE KEY
• The UNIQUE constraint ensures that all values in a column are
different.
• Both the UNIQUE and PRIMARY KEY constraints provide a
guarantee for uniqueness for a column or set of columns.
• A PRIMARY KEY constraint automatically has
a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
• Specifies alternate (secondary) keys
Specifying constraints in SQL
2)Specifying key and referential integrity constraint contd..
UNIQUE KEY
On create table
CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL,USN CHAR(8) UNIQUE ,SEM INT DEFAULT
1);
or
CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL,USN CHAR(8) ,SEM INT DEFAULT
1,CONSTRAINT UNI_USN UNIQUE(USN)); //Giving constraint name
On existing table
CREATE TABLE STUDENT(NAME VARCHAR(20) NOT NULL,USN CHAR(8) ,SEM INT DEFAULT 1);
ALTER TABLE STUDENT ADD UNIQUE(USN);
Or
ALTER TABLE STUDENT MODIFY USN CHAR(8) UNIQUE
or
ALTER TABLE STUDENT ADD CONSTRAINT UNI_USN UNIQUE(USN); //Giving constraint name
INSERT INTO STUDENT(“JESSICA’,’2BA16CS009’,2);
INSERT INTO STUDENT(“JESSICA’,’2BA16CS009’,2); //ERROR
Specifying constraints in SQL
2)Specifying key and referential integrity
constraint contd..
PRIMARY KEY CONSTRAINT
• The PRIMARY KEY constraint uniquely
identifies each record in a table.
• Primary keys must contain UNIQUE values,
and cannot contain NULL values.
• A table can have only ONE primary key; and in
the table, this primary key can consist of
single or multiple columns (fields).
2)
Specifying constraints in
Specifying key and referential integrity constraint contd..
SQL
Create Table
PRIMARY KEY
OR
CREATE TABLE STUDENT(NAME VARCHAR(20),USN CHAR(8),SEM INT
DEFAULT 1,CONSTRAINT USN_PK PRIMARY KEY(USN)); //Giving
constraint name
Or
CREATE TABLE STUDENT(NAME VARCHAR(20),sem int);
ALTER TABLE STUDENT add USN char(8) PRIMARY KEY
//col usn added with pkconstrint
Specifying constraints in SQL
2)Specifying key and referential integrity constraint contd..
Giving Constraint Name
On Existing Table
CREATE TABLE STUDENT(NAME VARCHAR(20),USN CHAR(8),SEM INT
DEFAULT 1);
Where
<attribute list>-list attribute names whose values are to be
retrieved.
< table list>-List of table names or relation names to process
query
SELECT DOB,ADDRESS
FROM EMPLOYEE
WHERE FNAME=‘JHON’ AND MNAME=‘B’ AND
LNAME=SMITH’
Basic Retrieval Queries In SQL contd..
Examples:
2)Retrieve the name of employees who either work for
department 4 and make over salary 30000 or work for
department 5 and make over salary 28000
SELECT FNAME,MNAME,LNAME
FROM EMPLOYEE
WHERE (DN=4 AND SALARY>30000) OR(DN=5 AND
SALARY>28000)
Basic Retrieval Queries In SQL contd..
TRY TO SOLVE
1)Retrieve the ssn of employee who works on project
more than 20 hours
2)Retrieve the dependent name of employee whose
ssn is 333445555
3)Retrieve the project number and project location of
controlling department 5
4) Retrieve the department location department 5
Basic Retrieval Queries In SQL contd..
Ambiguous attribute names, aliasing, renaming and tuple variables
In SQL same name can be used for two or more attributes in different
tables
Then qualify the attribute name with the relation name to prevent
ambiguity.
1)Retrieve the fname ,lname and address of all employees who work for the
Research department
SELECT FNAME,LNAME,ADDTESS
FROM EMPLOYEE,DEPARTMENT
WHERE DEPARTMENT.DNAME=‘RESEARCH’ AND
DEPARTMENT.DNO=EMPLOYEE.DNO
A query that involves selection, projection attributes and join conditions is
called select-project-join query
Basic Retrieval Queries In SQL contd..
Ambiguous attribute names, aliasing, renaming and
tuple variables
Ambiguity of attribute names also arises if queries that
refer to same relation twice
Then aliasing can be used.
In following example E and S can be used as aliases
1)For each employee retrieve the employees first and last
name and first and last name of his or her immediate
supervisor
SELECT E.FNAME,E.LNAME, S.FNAME,S.LNAME
FROM EMPLOYEE E,EMPLOYEE S
WHERE E.SSSN=S.SSN
Basic Retrieval Queries In SQL contd..
Try to solve.
Retrieve the fname and lname of the employee who
is manager for the department located at Houstan.
Retrieve the fname and lname of the employee
whose dependent name is Alice .
Retrieve the name of the department which is
controlling the projects Product X and
computerization .
Retrieve the name of employees who are working on
project number 3
Basic Retrieval Queries In SQL contd..
Unspecified where clause and use of the asterisk.
Unspecified where clause
-A missing WHERE clause indicates no condition on
tuple selection
-Hence all tuples of the relation specified in the FROM
clause qualify and are selected from the query result
-If more than one relation is specified FROM clause and
there is no WHERE clause then the cross product –all
possible tuple combinations of these relation is
selected
Basic Retrieval Queries In SQL contd..
Unspecified where clause and use of the asterisk contd..
Unspecified where clause examples
1)Retrieve all employee ssn
SELECT SSN
FROM EMPLOYEE
2)Retrieve name of employee and department name ofall
combination of employee and department database
SELECT SSN,DNAME
FROM EMPLOYEE,DEPARTMENT
Basic Retrieval Queries In SQL contd..
Unspecified where clause and use of the asterisk contd…
use of the asterisk.
Asterisk is used to retrieve all the attribute values
1)Retrieve details of employee who works for department 5
SELECT *
FROM EMPLOYEE
WHERE DNO=5
2) Retrieve all details of employee who works for department Research
SELECT *
FROM EMPLOYEE,DEPARTMENT
WHERE DNAME=‘Research’ AND EMPLOYEE.DNO=DEPARTMENT.DNO
3) Retrieve all combination of details of employee with department
SELECT *
FROM EMPLOYEE,DEPARTMENT
Basic Retrieval Queries In SQL contd..
Tables as set in SQL
-Usually treats a table as a set not a set but as multiset
-Therefore duplicate tuples appear more than once in a
table and in the result of a query
-SQL dose not automatically eliminate duplicate tuples in
the result of queries for the following reasons
• Duplication elimination is expensive operation. One
way to implement is sort the tuples first and the
eliminate duplicates
• User may want to see duplicate tuples in the result
query
• when an aggregate function is applied to tuples
Basic Retrieval Queries In SQL contd..
Tables as set in SQL contd..
1)DISTINCT keyword can be used to eliminate duplicate
tuples from relation
Retrieve the all distinct salary of employee
SELECT DISTINCT SALARY
FROM EMPLOYEE.
2)ALL keyword doesn’t eliminate duplicate tuples from
relation
Retrieve the all salary of employee
SELECT ALL SALARY
FROM EMPLOYEE.
Basic Retrieval Queries In SQL contd..
Tables as set in SQL contd..
Try to solve
1)Retreive distinct salaries of employees who is working for
department 5;
SELECT DISTINCT SALARY
FROM EMPLOYEE
WHERE DNO=5
SELECT E.DNO,D.DNAME
FROM EMPLOYEE E,DEPARTMENT D
WHERE E.DNO=D.DNUM
ORDER BY D.DNAME,E.FNAME DESC
2) Retrieve name, department name and department location
of employees working for department order by department
number ,first name,dlocation
Basic Retrieval Queries In SQL contd..
INSERT INTO
EMPLOYEE(FNAME,MNAME,LNAME,SSN,BDATE,ADDRESS,GENDER,SALARY,SSSN,DNO)
VALUES(:FNAME,:MNAME,:LNAME,SSN,:BDATE,:ADDRESS,G:ENDER,:SALARY,:SSSN,:DNO)
Last insert example Used in Oracle 11g to read input from keyboard
Insert,Delete and update statements in SQL
Delete
-Used to delete tuples from relation
-Has where clause to select the tuples to be deleted
-Deletion may be propagated if referential triggered actions are
specified in the referential integrity constraint of the DDL.
1)Delete the employee tuple whose last name is Brown
DELETE FROM EMPLOYEE
WHERE LNAME=‘Brown’
2) Delete the employee tuple who is working for the department dno
5
DELETE FROM EMPLOYEE
WHERE DNO=5
3) Delete the employee tuple who is working for the department
located at Stafford
DELETE FROM EMPLOYEE E,DEPT_LOCATION D
Insert,Delete and update statements in SQL
Update
-Used to modify attribute values of one or more selected tuples
-Has where clause to select the tuples to be updated
-Updating primary key value msay propagate to the foreign key values of tuples of
other relations if referential triggered actions are specified.
1)Update the employee salary by incrementing 5% of salary who is working for
dno 5
UPDATE EMPLOYEE
SET SALARY=SALARY+SALARY*0.5
WHERE DNO=5
2) Update the employee fname to Jack whose ssn is 333445555
UPDATE EMPLOYEE
SET FNAME=‘Jack’
WHERE SSN= 333445555
-It is also possible to specify NULL or default as the new attribute value