Presentation is loading. Please wait.

Presentation is loading. Please wait.

FEN 2015-08-311 Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.

Similar presentations


Presentation on theme: "FEN 2015-08-311 Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases."— Presentation transcript:

1 FEN 2015-08-311 Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases

2 SQL SQL is a realisation of the relational model. SQL is much more than merely queries – it includes: DDL Data Definition Language DML Data Manipulation Language DCL Data Control Language FEN 2015-08-312

3 SQL-Versions SQL has been implemented by many different DBMS-manufactures SQL is to a large extend the same for most DBMSs – close to a de facto standard Standards: SQL86 (SQL1), SQL89 (SQL1½), SQL92 (SQL2), SQL3 (SQL9x/SQL2000? - eventually SQL-99) SQL2 is still the most common standard. SQL-99 (Huge - released in 2002) Now SQL:2003 (partly supported by MS SQL Server 2008,revisions SQL:2008, SQL:2011) Most manufactures have their own extensions (and omissions) to the standard FEN 2015-08-313 For instance: Oracle MySQL MS SQL Server PostgreSQL For instance: Oracle MySQL MS SQL Server PostgreSQL ??? If you are confused – it’s for a good reason. But in practice SQL2 is still most used, the rest is mostly extensions. ??? If you are confused – it’s for a good reason. But in practice SQL2 is still most used, the rest is mostly extensions.

4 Example: Company - Schema FEN 2015-08-314

5 Example: Company - Sample Data FEN 2015-08-315

6 Example: Company - Foreign Key Constraints FEN 2015-08-316

7 Company on SQL Server Let’s see it work: MS SQL Server Did you note the order of table creation? Did you note the order of inserting sample data? FEN 2015-08-317

8 Company on SQL Server Do we miss a foreign key constraint here: Let’s try to make an error: change mgrssn to a not existing ssn. Why didn’t we add a constraint when the table was created? Solution: ALTER TABLE – let’s try. FEN 2015-08-318

9 SQL Data Definition Language - Alter Table DROP SCHEMA DROP TABLE ALTER TABLE ADD (column) DROP COLUMN ALTER TABLE DROP CONSTRAINT ADD CONSTRAINT FEN 2015-08-319

10 SQL: Data Manipulation Language SELECT UPDATE INSERT DELETE FEN 2015-08-3110 Work on tables

11 Queries: SELECT Syntax: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] [...]:WHERE, GROUP BY, HAVING and ORDER BY may be omitted. FEN 2015-08-3111

12 Examples: Company (Q0): Row and column selection: SELECTBdate, Address FROMEmployee WHEREFname= ’John’ AND Minit = ’B’ AND Lname = ’Smith’ All attributes: SELECT * --- FEN 2015-08-3112

13 Examples: Company (Q1): JOIN: SELECT Fname, Lname, Address FROMEmployee, Department WHEREDname = ’Research’ AND Dno = Dnumber Last term in the WHERE-clause is the join-condition. If omitted the result will be the Cartesian product. Alternative syntax is possible. FEN 2015-08-3113

14 Examples: Company (Q2): JOIN several tables: SELECTPnumber, Dnum, Lname, Address FROMProject, Employee, Department WHEREPlocation = ’Stafford’ AND Dnum = Dnumber AND Ssn = Mgrssn Note: Two join-conditions in the WHERE-clause. FEN 2015-08-3114

15 Examples: Company (Q8): Ambiguous attribute names and aliases: SELECTE.Fname, E.Lname, S.Fname, S.Lname FROMEmployee E, Employee S WHEREE.Superssn= S.Ssn Employee is joined with itself using the aliases E and S. ’.’ (”dot”)-notation may also be used to resolve ambiguous attribute Names (remember Minibank?). FEN 2015-08-3115

16 Examples: Company SQL-tables are NOT sets (in the math sense of the word set): ( Q11):SELECT Salary FROM Employee (Q11A):SELECT DISTINCT Salary FROM Employee FEN 2015-08-3116

17 Examples: Company SQL-tables are NOT sets, but in set operations (UNION, INTERSECT and EXCEPT) they are: (SELECT PNUMBER FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE LNAME = ’Smith’ AND DNUM = DNUMBER AND MGRSSN = SSN ) UNION (SELECT PNUMBER FROM PROJECT, WORKS_ON, EMPLOYEE WHERE LNAME = ’Smith’ AND PNO = PNUMBER AND ESSN = SSN) FEN 2015-08-3117

18 Updates i SQL: Updates: Inserting rows:INSERT Deleting rows:DELETE Updating row values:UPDATE As SELECT they work on tables. FEN 2015-08-3118

19 Examples: Company Inserting a single row: INSERT INTO EMPLOYEE VALUES (’Richard’,’K’,’Marini’,’653298653’, ’30-DEC-52’,’98 Oak Forest, Katy, ’TX’,’M’,37000,’987654321’,4) Inserting a single row, selected attributes: INSERT INTO EMPLOYEE(FNAME,LNAME,SSN) VALUES(’Richard’,’Marini’,’653298653’) Is rejected if any of the other attributes is defined NOT NULL and doesn’t have defined a default value. FEN 2015-08-3119

20 Examples: Company Deleting rows: DELETE FROMEMPLOYEE WHERELNAME =’Brown’ DELETE FROMEMPLOYEE WHERESSN = ’123456789’ DELETE FROMEMPLOYEE WHEREDNO IN (SELECTDNUMBER FROMDEPARTMENT WHEREDNAME = ’Research’) DELETE FROMEMPLOYEE (Not equivalent to: ´DROP TABLE EMPLOYEE’. Why not?) FEN 2015-08-3120

21 Examples: Company Updating rows: UPDATEPROJECT SETPLOCATION = ’Bellaire’, DNUM = 5 WHEREPNUMBER = 10 UPDATEEMPLOYEE SETSALARY = SALARY*1.1 WHEREDNO IN(SELECTDNUMBER FROMDEPARTMENT WHEREDNAME = ’Research’) Note, that it is only possible to affect one table in one UPDATE statement. FEN 2015-08-3121


Download ppt "FEN 2015-08-311 Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases."

Similar presentations


Ads by Google