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

Embed Size (px)

DESCRIPTION

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 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.

Citation preview

FEN Introduction to the database field: SQL: Structured Query Language Seminar: Introduction to relational databases 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 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 For instance: Oracle MySQL MS SQL Server PostgreSQL For instance: Oracle MySQL MS SQL Server PostgreSQL ??? If you are confused its for a good reason. But in practice SQL2 is still most used, the rest is mostly extensions. ??? If you are confused its for a good reason. But in practice SQL2 is still most used, the rest is mostly extensions. Example: Company - Schema FEN Example: Company - Sample Data FEN Example: Company - Foreign Key Constraints FEN Company on SQL Server Lets see it work: MS SQL Server Did you note the order of table creation? Did you note the order of inserting sample data? FEN Company on SQL Server Do we miss a foreign key constraint here: Lets try to make an error: change mgrssn to a not existing ssn. Why didnt we add a constraint when the table was created? Solution: ALTER TABLE lets try. FEN SQL Data Definition Language - Alter Table DROP SCHEMA DROP TABLE ALTER TABLE ADD (column) DROP COLUMN ALTER TABLE DROP CONSTRAINT ADD CONSTRAINT FEN SQL: Data Manipulation Language SELECT UPDATE INSERT DELETE FEN Work on tables Queries: SELECT Syntax: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] [...]:WHERE, GROUP BY, HAVING and ORDER BY may be omitted. FEN Examples: Company (Q0): Row and column selection: SELECTBdate, Address FROMEmployee WHEREFname= John AND Minit = B AND Lname = Smith All attributes: SELECT * --- FEN 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 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 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 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 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 Updates i SQL: Updates: Inserting rows:INSERT Deleting rows:DELETE Updating row values:UPDATE As SELECT they work on tables. FEN Examples: Company Inserting a single row: INSERT INTO EMPLOYEE VALUES (Richard,K,Marini, , 30-DEC-52,98 Oak Forest, Katy, TX,M,37000, ,4) Inserting a single row, selected attributes: INSERT INTO EMPLOYEE(FNAME,LNAME,SSN) VALUES(Richard,Marini, ) Is rejected if any of the other attributes is defined NOT NULL and doesnt have defined a default value. FEN Examples: Company Deleting rows: DELETE FROMEMPLOYEE WHERELNAME =Brown DELETE FROMEMPLOYEE WHERESSN = DELETE FROMEMPLOYEE WHEREDNO IN (SELECTDNUMBER FROMDEPARTMENT WHEREDNAME = Research) DELETE FROMEMPLOYEE (Not equivalent to: DROP TABLE EMPLOYEE. Why not?) FEN 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