0% found this document useful (0 votes)
15 views15 pages

Chapter 7

Uploaded by

Abinet Arba
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)
15 views15 pages

Chapter 7

Uploaded by

Abinet Arba
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/ 15

Chapter 7

Structured Query Language(SQL)

Contents
Data Definition Language
Data Manipulation Language
Basic SQL Queries

1
SQL Data Definition
• SQL uses the terms table, row, and column for the formal
relational model terms relation, tuple, and attribute,
respectively.
• The main SQL command for data definition is the CREATE
statement, which can be used to create schemas, tables
(relations), and domains.
The CREATE TABLE Command in SQL
 The CREATE TABLE command is used to specify a new
relation by giving it a name and specifying its attributes and
initial constraints or their data types (INTEGER, FLOAT,
DECIMAL(i.j), CHAR(n), VARCHAR(n)).
 The attributes are specified first, and each attribute is given a
name, a data type to specify its domain of values, and any
attribute constraints, such as NOT NULL. 2
Example for CREATE TABLE
1. CREATE TABLE EMPLOYEE
( Fname VARCHAR(15) NOT NULL,
Minit CHAR,
Lname VARCHAR(15) NOT NULL,
Ssn CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(30),
Sex CHAR,
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT NOT NULL,
PRIMARY KEY (Ssn),
FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber) ); 3
Con’t…
2. CREATE TABLE DEPARTMENT
( Dname VARCHAR(15) NOT NULL,
Dnumber INT NOT NULL,
Mgr_ssn CHAR(9) NOT NULL,
Mgr_start_date DATE,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn) REFERENCES
EMPLOYEE(Ssn) );

4
SQL Data Manipulation Language

• The Data Manipulation Language (DML) is used to retrieve,


insert and modify database information.
• These commands will be used by all database users during the
routine operation of the database.
• In SQL, three commands can be used to modify the database:
INSERT, DELETE, and UPDATE. We discuss each of these in
turn.
Basic SQL Queries
Retrieval Queries in SQL
SQL has one basic statement for retrieving information from a
database; the SELECT statement.

5
Con’t…
• 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

6
Employee table

7
Department table

8
Con’t…
• Query 0: Retrieve the birthdate and address of the employee
whose name is 'John B. Smith'.

Q0:SELECT BDATE, ADDRESS


FROM EMPLOYEE
WHERE FNAME='John' AND MINIT='B’
AND LNAME='Smith’;

• Query 1: Retrieve the name and address of all employees who


work for the 'Research' department.

Q1:SELECT FNAME, LNAME, ADDRESS


FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND
DNUMBER=DNO;
9
A. INSERT Command
• In its simplest form, it is used to add one or more tuples to a relation.
• Attribute values should be listed in the same order as the attributes were
specified in the CREATE TABLE command.
Example:
INSERT INTO EMPLOYEE
VALUES ('Richard','K','Marini', '653298653', '30-DEC-52','98
Oak Forest,Katy,TX', 'M', 37000,'987654321', 4 );

• An alternate form of INSERT specifies explicitly the attribute names that


correspond to the values in the new tuple.
• Attributes with NULL values can be left out.
Example: Insert a tuple for a new EMPLOYEE for whom we only know the
FNAME, LNAME, and SSN attributes.

INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)


VALUES ('Richard', 'Marini', '653298653');

10
B. DELETE Command
• Removes tuples from a relation.
• Includes a WHERE-clause to select the tuples to be deleted
Tuples are deleted from only one table at a time.

• A missing WHERE-clause specifies that all tuples in the


relation are to be deleted; the table then becomes an empty
table.

• The number of tuples deleted depends on the number of tuples


in the relation that satisfy the WHERE-clause.

11
Con’t…
• Examples:

A: DELETE FROM EMPLOYEE


WHERE LNAME='Brown’;

B: DELETE FROM EMPLOYEE


WHERE SSN='123456789’;

C: DELETE FROM EMPLOYEE


WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research');

D: DELETE FROM EMPLOYEE;

12
C. UPDATE Command
• Used to modify attribute values of one or more selected tuples.
• A WHERE-clause selects the tuples to be modified.
• An additional SET-clause specifies the attributes to be
modified and their new values.
• Each command modifies tuples in the same relation.
• Example: Change the location and controlling department
number of project number 10 to 'Bellaire' and 5, respectively.

U5: UPDATE PROJECT


SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER=10;

13
Con’t…
• Example: Give all employees in the 'Research' department a
10% raise in salary.

U6: UPDATE EMPLOYEE


SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research');

14
THE END!!

15

You might also like