Presentation is loading. Please wait.

Presentation is loading. Please wait.

31-08-2007NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.

Similar presentations


Presentation on theme: "31-08-2007NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML."— Presentation transcript:

1 31-08-2007NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML

2 31-08-2007NOEA/IT - FEN: Databases/SQL2 Realisation of the Relational Model: SQL-based DBMSs (SQL = Structured Query Language) SQL is much more than merely queries – it includes –DDL Data Definition Language –DML Data Manipulation Language –DCL Data Control Language

3 31-08-2007NOEA/IT - FEN: Databases/SQL3 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 is the newest standard (released in 2002) Most manufactures have their own extensions (and omissions) to the standard

4 31-08-2007NOEA/IT - FEN: Databases/SQL4 SQL2 - DDL Supports domains: –Basic types, INT, CHAR, strings etc. –Date-Time –Programmer defined types: CREATE DOMAIN cprType AS CHAR(10); CREATE DOMAIN postalType AS CHAR(4); Types allows the compiler/interpreter to check for some logical errors Not in MS SQL Server 2000

5 31-08-2007NOEA/IT - FEN: Databases/SQL5 SQL2 - DDL CREATE TABLE Client ( cprnocprTypeNOT NULL, …. postCodepostalType, …, CONSTRAINT ClientPK PRIMARY KEY(cprno), CONSTRAINT PCodeCityFK FOREIGN KEY(postCode) REFERENCES PCodeCity(pCode) ON UPDATE CASCADE ON DELETE SET NULL); Should make NOT NULL unnecessary

6 31-08-2007NOEA/IT - FEN: Databases/SQL6 SQL2 - DDL CREATE TABLE PCodeCity ( pCodepostalTypeNOT NULL, cityVARCHAR(30)NOT NULL, CONSTRAINT PCodeCityPK PRIMARY KEY(pCode)); Also see Elmasri figure 8.1 and 8.2

7 31-08-2007NOEA/IT - FEN: Databases/SQL7 SQL2 - DDL DROP SCHEMA DROP TABLE ALTER TABLE –DROP CONSTRAINT –ADD CONSTRAINT

8 31-08-2007NOEA/IT - FEN: Databases/SQL8 SQL2 - DML SELECT UPDATE INSERT DELETE

9 31-08-2007NOEA/IT - FEN: Databases/SQL9 SQL2 – DML: SELECT Queries: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] [...]:WHERE, GROUP BY, HAVING and ORDER BY may be omitted.

10 31-08-2007NOEA/IT - FEN: Databases/SQL10 The Company Database

11 31-08-2007NOEA/IT - FEN: Databases/SQL11 Company on SQL Server Script:

12 31-08-2007NOEA/IT - FEN: Databases/SQL12 Relational Algebra - Overview

13 31-08-2007NOEA/IT - FEN: Databases/SQL13 SQL2 - DML (Q0): Row and coulomb selection: SELECTBDATE, ADDRESS FROMEMPLOYEE WHEREFNAME = ’John’ AND MINIT = ’B’ AND LNAME = ’Smith’ All attributes: SELECT * ----

14 31-08-2007NOEA/IT - FEN: Databases/SQL14 SQL2 - DML (Q1): JOIN: SELECTFNAME, 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.

15 31-08-2007NOEA/IT - FEN: Databases/SQL15 SQL2 - DML (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.

16 31-08-2007NOEA/IT - FEN: Databases/SQL16 SQL2 - DML (Q8): Ambiguous attribute names and aliases: SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROMEMPLOYEE E, EMPLOYEE S WHEREE.SUPERSNN = S.SSN EMPLOYEE is joined with itself using the aliases E and S. ’.’ (”dot”)-notation may also be used to resolve ambiguous attribute names.

17 31-08-2007NOEA/IT - FEN: Databases/SQL17 SQL2 - DML SQL-tables are NOT sets (in the math sense of the word set): (Q11):SELECTSALARY FROMEMPLOYEE (Q11A):SELECT DISTINCT SALARY FROMEMPLOYEE

18 31-08-2007NOEA/IT - FEN: Databases/SQL18 SQL2 - DML (Q4): Set operations: (SELECTPNUMBER FROMPROJECT, DEPARTMENT, EMPLOYEE WHERELNAME = ’Smith’ AND DNUM = DNUMBER AND MGRSSN = SSN ) UNION (SELECTPNUMBER FROMPROJECT, WORKS_ON, EMPLOYEE WHERELNAME = ’Smith’ AND PNO = PNUMBER AND ESSN = SSN) Returns a set!!! (also INTERSECT and EXCEPT)

19 31-08-2007NOEA/IT - FEN: Databases/SQL19 SQL2 - DML Updates: –Inserting rows:INSERT –Deleting rows:DELETE –Updating row values:UPDATE As SELECT they work on tables.

20 31-08-2007NOEA/IT - FEN: Databases/SQL20 SQL2 - DML Inserting a single row: INSERT INTOEMPLOYEE VALUES(’Richard’,’K’,’Marini’,’653298653’, ’30-DEC-52’,’98 Oak Forest, Katy, ’TX’,’M’,37000,’987654321’,4) Inserting a single row, only selected attributes: INSERT INTOEMPLOYEE(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.

21 31-08-2007NOEA/IT - FEN: Databases/SQL21 SQL2 - DML Inserting using sub SELECTs: CREATE TABLE DEPTS_INFO(DEPT_NAMEVARCHAR(15), NO_OF_EMPSINTEGER, TOTAL_SALINTEGER); INSERT INTODEPTS_INFO SELECTDNAME, COUNT(*), SUM(SALARY) FROMDEPARTMENT, EMPLOYEE WHEREDNUMBER = DNO GROUP BYDNAME; Note DEPTS_INFO is not automatically updated if changes are made in the othe tables. It is a base table.

22 31-08-2007NOEA/IT - FEN: Databases/SQL22 SQL2 - DML 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?)

23 31-08-2007NOEA/IT - FEN: Databases/SQL23 SQL2 - DML 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.

24 31-08-2007NOEA/IT - FEN: Databases/SQL24 Exercises 1.The Company Database: 1.Run the CREATE-scripts on MS SQL ServerCREATE-scripts 2.Do some of examples in Elmasri chapter 8 2.To which extend does SQL Server obey to the standard? Can you find any divergences? 3.The VW Database: 1.Run the CREATE-scriptsCREATE-scripts 2.Implement the queries from vwDatabase.pdf in SQLvwDatabase.pdf


Download ppt "31-08-2007NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML."

Similar presentations


Ads by Google