Download الشريحة 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Microsoft Access wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Null (SQL) wikipedia , lookup

Join (SQL) wikipedia , lookup

Database model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Relational model wikipedia , lookup

Transcript
Fundamentals of
Database Systems
Fourth Edition
El Masri & Navathe
Chapter 8
SQL-99: Schema Definition,
Basic Constraints, and Queries
Ihr Logo
http://mohammedshbier.wordpress.com
Your Logo
Definition
SQL commonly expanded as Structured Query Language,
is a computer language designed for :
1.
2.
3.
Retrieval and management of data in relational database
management systems.
Database schema creation and modification
Database object access control management.
Although SQL is both an ANSI and an ISO standard, many
database products support SQL with proprietary
extensions to the standard language. Queries take the
form of a command language that lets you select, insert,
update, find out the location of data, and so forth.
3
Your Logo
SQL: CREATE TABLE
•Specifies a new base relation by giving it a name, and
specifying each of its attributes and their data types
(INTEGER, FLOAT, DECIMAL(i,j), CHAR(n),
VARCHAR(n)).
•A constraint NOT NULL may be specified on an attribute.
CREATE TABLE DEPARTMENT
(
DNAME
VARCHAR2(10)NOT NULL,
DNUMBER
INTEGER
NOT NULL,
MGRSSN
CHAR(9),
MGRSTARTDATE
CHAR(9) );
4
Your Logo
SQL: CREATE TABLE (Cont.)
•In SQL2, can use the CREATE TABLE command for specifying the
primary key attributes, secondary keys, and referential integrity
constraints (foreign keys).
•Key attributes can be specified via the PRIMARY KEY and
UNIQUE phrases
CREATE TABLE DEPT
(
DNAME VARCHAR2(10) NOT NULL,
DNUMBER
INTEGER
NOT NULL,
MGRSSN
CHAR(9),
MGRSTARTDATE
CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP (SSN );
5
Your Logo
SQL: DROP TABLE
•
•
Used to remove a relation (base table) and its
definition
The relation can no longer be used in queries,
updates, or any other commands since its
description no longer exists
Example:
DROP TABLE DEPENDENT;
6
Your Logo
SQL: ALTER TABLE
•
•
7
The ALTER TABLE command allows you to add,
modify, or drop a column from an existing table
The new attribute will have NULLs in all the tuples
of the relation right after the command is executed;
hence, the NOT NULL constraint is not allowed for
such an attribute
Your Logo
SQL: ALTER TABLE (Cont.)
Adding column(s) to a table
Syntax #1
•To add a column to an existing table, the ALTER TABLE
syntax is:
ALTER TABLE table_name
ADD column_name column-definition;
•For example:
ALTER TABLE supplier
ADD supplier_name varchar2(50);
This will add a column called supplier_name to the
supplier table.
8
Your Logo
SQL: ALTER TABLE (Cont.)
Syntax #2
To add multiple columns to an existing table, the ALTER
TABLE syntax is:
ALTER TABLE table_name ADD (
column_1 column-definition,
column_2 column-definition,
...
column_n column_definition );
For example:
ALTER TABLE supplier ADD (
supplier_name varchar2(50),
city varchar2(45) );
This will add two columns (supplier_name and city) to
the supplier table.
9
Your Logo
SQL: ALTER TABLE (Cont.)
Modifying column(s) in a table
Syntax #1
•To modify a column in an existing table, the ALTER
TABLE syntax is:
ALTER TABLE table_name
MODIFY column_name column_type;
•For example:
ALTER TABLE supplier
MODIFY supplier_name varchar2(100)
not null;
This will modify the column called supplier_name to be a data type
of varchar2(100) and force the column to not allow null
values.
10
Your Logo
SQL: ALTER TABLE (Cont.)
Modifying column(s) to a table
Syntax #2
•To modify multiple columns in an existing table, the
ALTER TABLE syntax is:
ALTER TABLE table_name MODIFY (
column_1 column_type,
column_2 column_type,
...
column_n column_type );
•For example:
ALTER TABLE supplier MODIFY (
supplier_name varchar2(100) not null,
city varchar2(75) );
11
Your Logo
SQL: ALTER TABLE (Cont.)
Drop column(s) in a table
Syntax #1
To drop a column in an existing table, the ALTER
TABLE syntax is:
ALTER TABLE table_name
DROP COLUMN column_name;
For example:
ALTER TABLE supplier
DROP COLUMN supplier_name;
This will drop the column called supplier_name
from the table called supplier.
12
Your Logo
SQL: REFERENTIAL INTEGRITY OPTIONS
We can specify CASCADE, SET NULL or SET DEFAULT on
referential integrity constraints (foreign keys)
CREATE TABLE DEPT
(DNAME
VARCHAR(10) NOT NULL,
DNUMBER
INTEGER
NOT NULL,
MGRSSN
CHAR(9)
DEFAULT 1,
MGRSTARTDATE
CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
ON DELETE SET DEFAULT
ON UPDATE CASCADE );
13
Your Logo
SQL: REFERENTIAL INTEGRITY OPTIONS (Cont.)
CREATE TABLE EMP
(
ENAME
VARCHAR(30) NOT NULL,
ESSN
CHAR(9),
BDATE
DATE,
DNO
INTEGER DEFAULT 1,
SUPERSSN
CHAR(9),
PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT
ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL
ON UPDATE CASCADE );
14
Your Logo
SQL: Some Data Types in SQL2 and SQL-99
.
Data Type
integer
numeric
Syntax
integer
Where p is a precision value; s is a scale value. For
numeric(p,s) example, numeric(6,2) is a number that has 4 digits
before the decimal and 2 digits after the decimal.
real
float
real
float(p)
character
char(x)
character varying
bit
date
time
timestamp
15
Explanation (if applicable)
varchar2(x)
bit(x)
date
time
timestamp
Single-precision floating point number
Where p is a precision value.
Where x is the number of characters to store. This
data type is space padded to fill the number of
characters specified.
Where x is the number of characters to store. This
data type does NOT space pad.
Where x is the number of bits to store.
Stores year, month, and day values.
Stores the hour, minute, and second values.
Stores year, month, day, hour, minute, and second
values.
Your Logo
Retrieval Queries in SQL
•
•
•
•
16
SQL has one basic statement for retrieving information
from a database; the SELECT statement
Important distinction between SQL and the formal
relational model; SQL allows a table (relation) to have
two or more tuples that are identical in all their attribute
values
Hence, an SQL relation (table) is a multi-set
(sometimes called a bag) of tuples; it is not a set of
tuples
SQL relations can be constrained to be sets by
specifying PRIMARY KEY or UNIQUE attributes, or
by using the DISTINCT optionYour
in Logo
a query
Retrieval Queries in SQL (Cont.)
•Basic form of the SQL SELECT statement is called a
mapping or a SELECT-FROM-WHERE block
SELECT
<attribute list>
FROM
<table list>
WHERE
<condition>
•<attribute list> is a list of attribute names whose values are to
be retrieved by the query
•<table list> is a list of the relation names required to process
the query
•<condition> is a conditional (Boolean) expression that
identifies the tuples to be retrieved by the query
17
Your Logo
Retrieval Queries in SQL (Cont.)
Note:
All queries from now are on the company database,
Book page No. :137.
18
Your Logo
Simple SQL Queries
•
•
•
•
Basic SQL queries correspond to using the SELECT,
PROJECT, and JOIN operations of the relational
algebra
All subsequent examples use the COMPANY database
Example of a simple query on one relation
Query 0: Retrieve the birthdate and address of the
employee whose name is 'John B. Smith'.
SELECT
19
BDATE, ADDRESS
FROM
EMPLOYEE
WHERE FNAME='John' AND MINIT='B’
AND
LNAME='Smith’;
Your Logo
Simple SQL Queries (Cont.)
•
Query 1: Retrieve the name and id of the projects which
are performed in Houston, Statford.
SELECT
PNAME, PNUMBER
FROM
PROJECT
WHERE
PLOCATION=‘Houston’
OR
PLOCATON=‘Statford’;
20
Your Logo
Simple SQL Queries (Cont.)
•
Query 2: Retrieve the full name of the employees who
are working in department No. 5 and have more than
1000$ monthly salary.
SELECT
FNAME, MINIT, LNAME
FROM
EMPLOYEE
WHERE
DNO=5
AND
SALARY>1000;
21
Your Logo
Simple SQL Queries (Cont.)
•
Query 3: Retrieve the name of each dependent who is a
son of some employee.
SELECT
DEPENDENT_NAME
FROM
DEPENDET
WHERE
RELATIONSHIP=‘SON’;
22
Your Logo
Simple SQL Queries (Cont.)
•
Query 4: Retrieve the name of each employee and
his/her department name.
SELECT
FNAME, MINIT, LNAME, DNAME
FROM
EMPLOYEE, DEPARTMENT
WHERE
DNO=DNUMBER;
23
Your Logo
Simple SQL Queries (Cont.)
•
Query 5: Retrieve the name and address of all
employees who work for the 'Research' department.
SELECT
FNAME, LNAME, ADDRESS
FROM
EMPLOYEE, DEPARTMENT
WHERE
DNO=DNUMBER
AND
DNAME='Research‘;
24
Your Logo
Simple SQL Queries (Cont.)
•
Query 6: Retrieve the name of each son and the name of
his father/mother name
SELECT
DEPENDENT_NAME
FROM
DEPENDENT, EMPLOYEE
WHERE
ESSN=SSN
AND
RELATIONSHIP=‘SON’;
25
Your Logo
Simple SQL Queries (Cont.)
•
Query 7: Retrieve the name of each department and the
name if its manager.
SELECT
DNAME, FNAME, MINIT, LNAME
FROM
DEPARTMENT, EMPLOYEE
WHERE
MGRSSN=SSN;
26
Your Logo
Simple SQL Queries (Cont.)
•Query 8: Retrieve the name of each project and the name
of its controlling department.
SELECT
PNAME, DNAME
FROM
PROJECT, DEPARTMENT,
WHERE
DNUM=DNUMBER;
27
Your Logo
Simple SQL Queries (Cont.)
•
Query 9: Retrieve the name managers.
SELECT
FROM
WHERE
28
FNAME, MINIT, LNAME
EMPLOYEE, DEPARTMENT
SSN=MGRSSN;
Your Logo
Simple SQL Queries (Cont.)
•
Query 10: Retrieve the name of each project and the
name of its controlling department and the name of the
department manager.
SELECT PNAME, DNAME, FNAME, MINIT, LNAME
FROM
PROJECT, DEPARTMENT, EMPLOYEE
WHERE
DNUM=DNUMBER
AND
MGRSSN=SSN;
29
Your Logo
Simple SQL Queries (Cont.)
•
Query 11: For every project located in 'Stafford', list the
project number, the controlling department number, and
the department manager's last name, address, and
birthdate..
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS
FROM
PROJECT, DEPARTMENT, EMPLOYEE
WHERE
DNUM=DNUMBER AND MGRSSN=SSN
AND
PLOCATION='Stafford’;
30
Your Logo
USE OF *
•
To retrieve all the attribute values of the selected
tuples, a * is used, which stands for all the
attributes
Examples:
SELECT
31
*
FROM
WHERE
SELECT
*
FROM
WHERE
AND
EMPLOYEE
DNO=5;
EMPLOYEE, DEPARTMENT
DNAME='Research‘
DNO=DNUMBER;
Your Logo