Академический Документы
Профессиональный Документы
Культура Документы
• Relational Constraints
• Keys
• Relational Algebra
• Structured Query Language (SQL)
Relational Constraints
• Application-Dependent Constraints
• Secondary key
– An attribute that is used for an index
• Search key
– An attribute that is used in a query
Relational Algebra
• A basis for implementation of any relational query language
• A procedural language
Relations for Examples
• Set-at-a-time language Emp1 eno name Emp2 eno name Work eno dept
• Unary Operations 1
2
JOHN
TOM
3
5
JOHN
MARY
1
4
SALES
SALES
– SELECT 3 JOHN 5 TOY
4 BOB
• Extracts tuples (rows) that satisfy a condition
• Notation condition(relation name)
• Example:
– Find all employees in the SALES department
» dept=“SALES”(Work) Work eno dept
1 SALES
– Find all employees that do not work for the TOY department and whose eno
4
isSALES
greater
than 2
» (eno>2) and (dept “TOY”)(Work)
– Can use any of the comparison operators (=, , <, , >, ) and
Work eno logical
dept operators (AND, OR,
NOT) 4 SALES
Relational Algebra
Relations for Examples
• SQL DDL is used for creating and destroying DB objects: –
Domains, Tables, Views, Indexes…
– Main SQL DDL statements are:
CREATE TABLE/ALTER TABLE DROP TABLE
CREATE VIEW DROP VIEW
CREATE INDEX DROP INDEX
Example:
CREATE TABLE Student (
sid CHAR(2),
firstName VARCHAR(15) ,
lastName VARCHAR(20) ,
login VARCHAR(7),
dob DATE
);
SQL
• Application-dependent constraints
– Domain constraint
• CHECK clause
SQL
• Another Example
create table employee(
employeeID number(5),
ssn char(9),
fName varchar(15)
lName varchar(15),
deptID number not null,
gender char(1),
constraint employee_pk primary key(employeeID),
constraint employee_fk1 foreign key (deptID) references
department(deptID) on delete restrict on update cascade,
constraint employee_uq1 unique (ssn),
constraint employee_ck1 check (gender IN (‘M’, ‘m’, ‘F’, ‘f’)));
SQL: DDL
• Examples
a) Give Manager full privileges to Staff table.
GRANT ALL PRIVILEGES
ON Staff
TO Manager WITH GRANT OPTION;
b) Give Personnel and Director SELECT and UPDATE on column salary of Staff.
GRANT SELECT, UPDATE (salary)
ON Staff
TO Personnel, Director;
c) Permit SELECT and UPDATE access privileges to all users
GRANT SELECT, UPDATE ON employee, project TO PUBLIC;
SQL: DML
• SELECT
– THE most important statement in the language, with
maximum options/variations to it.
– Implements 3 relational algebra operations
• Selection, Projection, Join
SQL: DML SELECT
• Examples:
a) SELECT AVG(e.age) FROM Employee e;
• INSERT – 2 formats
– INSERT INTO tablename[(column_list)]…..VALUES (value_list)…..
– INSERT INTO tablename[(column_list)]…..AS SELECT…..
• Examples
a) INSERT INTO Student(sid, name, login, age, gpa) VALUES (53688, ‘SMITH’,
‘SMITH@EE’, 18, 3.2);
b) INSERT INTO project VALUES(1234, ‘PERFECT PROJECT’, NULL, ‘JOHN’);
• Syntax
UPDATE tablename SET fieldname=VALUE
[WHERE fieldname comparisonOperator VALUE];
– If WHERE clause is specified, then only those rows that satisfy the
condition will be updated
– If WHERE clause is NOT specified, then ALL rows will be updated
• Examples:
• Syntax
DELETE FROM tablename
[WHERE fieldname comparisonOperator VALUE];
– If WHERE clause is specified, then only those rows that satisfy the
condition will be deleted
– If WHERE clause is NOT specified, then ALL rows will be deleted
• Examples: