Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax...
-
Upload
grace-walker -
Category
Documents
-
view
214 -
download
0
Transcript of Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax...
Introduction to SQL
1
Introduction to SQL
Lecture 5
Introduction to SQL
2
Note
in different implementations the syntax might slightly differ different features might be available
in certain implementations non-relational operators might be supported certain relational operations might not be
possible to be performed
Introduction to SQL
3
Operations
create table restrict, project and join (via SELECT) insert, update, delete create view, query view
Introduction to SQL
4
CREATE TABLE
CREATE TABLE Depts (Dept_id CHAR(2),Dept_name CHAR(20),Budget DECIMAL(8),
PRIMARY KEY (Dept_id));
Introduction to SQL
5
CREATE TABLE
CREATE TABLE Emps (E_id CHAR(2),E_name CHAR(20),Dept_id CHAR(2),Salary DECIMAL(5),
PRIMARY KEY (E_id), FOREIGN KEY (Dept_id) REFERENCES Depts);
Introduction to SQL
6
restrict (via SELECT)
SELECT ( E_id, E_name, Dept_id, Salary )FROM EmpsWHERE Salary > 33000;
Introduction to SQL
7
project (via SELECT)
SELECT ( E_name, Salary )FROM Emps ;
Introduction to SQL
8
join (via SELECT)
SELECT ( Depts.Dept_id, Dept_name, Budget, E_id, E_name, Salary )
FROM Emps, DeptsWHERE Depts.Dept_id = Emps.Dept_id ;
Introduction to SQL
9
restrict, project and join (via SELECT)
SELECT ( Dept_name, E_name, Salary )FROM Emps, DeptsWHERE Depts.Dept_id = Emps.Dept_id AND Salary > 33000;
Introduction to SQL
10
INSERT single row
INSERTINTO Emps ( E_id, E_name, Dept_id, Salary )VALUES ( ‘E1’, ‘Smith’, ‘D1’, 40000 ) ;
Introduction to SQL
11
INSERT multiple rows
INSERTINTO Temp ( Id, Name, Salary )
SELECT ( E_id, E_name, Salary)FROM EmpsWHERE Salary > 33000;
CREATE TABLE Temp (Id CHAR(2),Name CHAR(20),Salary DECIMAL(5),
PRIMARY KEY (Id) );
Introduction to SQL
12
UPDATE
UPDATE EmpsSET Salary = Salary + 900WHERE Salary < 40000;
Introduction to SQL
13
DELETE
DELETEFROM Depts WHERE Budget < 1000000;
Introduction to SQL
14
CREATE VIEW
CREATE VIEW All_emps ASSELECT (Dept_name, E_name, Salary)FROM Depts, EmpsWHERE Depts.Dept_id = Emps.Dept_id
Introduction to SQL
15
query a view
SELECT ( E_name, Salary )FROM All_empsWHERE Salary < 40000
Introduction to SQL
16
Intermediate conclusion
you now know a small set of operators by means of which you can create, query and modify a database; you can now implement your own database (even though, it probably will be far from a good design)
Introduction to SQL
17
Activity A4
You are the database administrator of your company. Your company is supplied with different parts by a set of supplying companies. You need to create and maintain a database with information about the supplied parts and the supplying companies.
1. given the ER (entity relationship) diagram, identify the attributes of each entity (NOTE: treat the relationship as an entity, i.e. find its attribute(s))
2. design the corresponding tables 3. write the data definition statements in SQL
Introduction to SQL
18
ER for the suppliers-parts problem
Supplier PartsContracted
Introduction to SQL
19
Conclusion
summary introduction to database systems introduction to the relational model introduction to SQL
from next lecture the relational model