Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax might slightly differ different features might be.

Similar presentations


Presentation on theme: "Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax might slightly differ different features might be."— Presentation transcript:

1 Introduction to SQL 1 Lecture 5

2 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

3 Introduction to SQL 3 Operations create table restrict, project and join (via SELECT) insert, update, delete create view, query view

4 Introduction to SQL 4 CREATE TABLE CREATE TABLE Depts ( Dept_idCHAR(2), Dept_nameCHAR(20), BudgetDECIMAL(8), PRIMARY KEY (Dept_id));

5 Introduction to SQL 5 CREATE TABLE CREATE TABLE Emps ( E_idCHAR(2), E_nameCHAR(20), Dept_idCHAR(2), SalaryDECIMAL(5), PRIMARY KEY (E_id), FOREIGN KEY (Dept_id) REFERENCES Depts);

6 Introduction to SQL 6 restrict (via SELECT) SELECT ( E_id, E_name, Dept_id, Salary ) FROM Emps WHERE Salary > 33000;

7 Introduction to SQL 7 project (via SELECT) SELECT ( E_name, Salary ) FROM Emps ;

8 Introduction to SQL 8 join (via SELECT) SELECT ( Depts.Dept_id, Dept_name, Budget, E_id, E_name, Salary ) FROM Emps, Depts WHERE Depts.Dept_id = Emps.Dept_id ;

9 Introduction to SQL 9 restrict, project and join (via SELECT) SELECT ( Dept_name, E_name, Salary ) FROM Emps, Depts WHERE Depts.Dept_id = Emps.Dept_id AND Salary > 33000;

10 Introduction to SQL 10 INSERT single row INSERT INTO Emps ( E_id, E_name, Dept_id, Salary ) VALUES ( E1, Smith, D1, 40000 ) ;

11 Introduction to SQL 11 INSERT multiple rows INSERT INTO Temp (Id, Name, Salary ) SELECT ( E_id, E_name, Salary) FROM Emps WHERE Salary > 33000; CREATE TABLE Temp ( IdCHAR(2), NameCHAR(20), SalaryDECIMAL(5), PRIMARY KEY (Id) );

12 Introduction to SQL 12 UPDATE UPDATE Emps SET Salary = Salary + 900 WHERE Salary < 40000;

13 Introduction to SQL 13 DELETE FROM Depts WHERE Budget < 1000000;

14 Introduction to SQL 14 CREATE VIEW CREATE VIEW All_emps AS SELECT (Dept_name, E_name, Salary) FROM Depts, Emps WHERE Depts.Dept_id = Emps.Dept_id

15 Introduction to SQL 15 query a view SELECT ( E_name, Salary ) FROM All_emps WHERE Salary < 40000

16 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)

17 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

18 Introduction to SQL 18 ER for the suppliers-parts problem SupplierParts Contracted

19 Introduction to SQL 19 Conclusion summary introduction to database systems introduction to the relational model introduction to SQL from next lecture the relational model


Download ppt "Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax might slightly differ different features might be."

Similar presentations


Ads by Google