Professional Documents
Culture Documents
Language.
Originally, SQL was called SEQUEL ( Structured
English QUEry Language)
SQL uses the terms table, row and column for the
formal relational model terms relation, tuple and
attribute.
The main SQL command for data definition is the
CREATE statement, which can be used to create
schemas, tables, types, domain as well as other
constructs.
SQL schema is identified by a schema name and
includes an authorization identifier to indicate the
user or account who owns the schema, as well as
descriptors for each element in the schema.
Schema elements includes tables, types,
constraints, views, domains, and other constructs
that describe the schema.
A schema is created via the CREATE SCHEMA
statement, which can include all the schema
elements definitions.
Example:
CREATE DOMAIN D_NUM AS INTEGER CHECK
(D_NUM > 0 AND D_NUM < 21);
Specifying Key and Referential Integrity
Constraints
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.
Dnumber INT PRIMARY KEY;
The UNIQUE clause specifies alternate
(secondary) keys, The UNIQUE constraint ensures
that all values in a column are different. Both the
UNIQUE and PRIMARY KEY constraints are similar.
A table can have many UNIQUE constraints , but
only one PRIMARY KEY constraint per table.
SELECT SSN
FROM EMPLOYEE
Query: SELECT *
FROM EMPLOYEE
WHERE DNO=5
Query: SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND
DNO=DNUMBER
SQL treats a table as multiset rather than as a set.
Duplicate tuples can appear more than once in a tuple.
SQL doesn’t remove duplicate tuples automatically in the
results of queries.
To eliminate duplicate tuples in a query result, the
keyword DISTINCT is used
For example, the result of Q may have duplicate SALARY
values whereas Q11A does not have any duplicate
values
Q: SELECT SALARY
FROM EMPLOYEE
QA: SELECT DISTINCT SALARY
FROM EMPLOYEE
SQL has directly incorporated some set operations
There is a union operation (UNION), and in some
versions of SQL there are set difference (EXCEPT)
and intersection (INTERSECT) operations
The resulting relations of these set operations are
sets of tuples; duplicate tuples are eliminated from
the result
The set operations apply only to union compatible
relations; the two relations must have the same
attributes and the attributes must appear in the
same order
Query : Make a list of all project numbers for projects
that involve an employee whose last name is 'Smith' as
a worker or as a manager of the department that
controls the project.
UNION
SELECT Fname,Lname
FROM EMPLOYEE
WHERE BDATE LIKE ‘_ _5 _ _ _ _ _ _ _’
Here, ‘5’ must be the third character of the string as per the
DATE format.
If an underscore or % is to be used as literal character in the
string, the character should be preceded by an escape
character.
Example: AB\_CD\%EF
The standard arithmetic operators '+', '-'. '*', and '/'
(for addition, subtraction, multiplication, and division,
respectively) can be applied to numeric values in an
SQL query result
Query : Show the effect of giving all employees who
work on the 'ProductX' project a 10% raise.
SELECT *
FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000)
AND Dno=5
UPDATE PROJECT
SET PLOCATION = 'Bellaire',
DNUM = 5
WHERE PNUMBER=10
Example:
Give all employees in the 'Research'
department a 10% raise in salary.
UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE
DNAME='Research')