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

Slides:



Advertisements
Similar presentations
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Advertisements

SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
COMPANY schema EMPLOYEE
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Database technology Lecture 2: Relational databases and SQL
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
SQL Winter 2006Ron McFadyen Outline: SQL DDL - creating schemas - modifying schemas DML - select-from-where clause - group by, having, order by.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
SQL: Schema Definition, Constraints, & Queries and Views.
Relational Algebra - Chapter (7th ed )
Onsdag The concepts in a relation data model SQL DDL DML.
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
CS 380 Introduction to Database Systems (Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views)
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
SQL Chapter 8. SQL or SEQUEL - (Structured English Query Language) Based on relational algebra Developed in 1970's released in early 1980's Standardized.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2004 Pearson Education, Inc. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –SQL (chapter 8) –Instructional.
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
1 Chapter 5 The SQL Language Data Definition Language The Data Definition Language (DDL) is used to create and destroy databases and database.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Introduction to Database Systems
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 6 Basic SQL Slide 6- 1.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
The SQL Database Grammar
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
376a. Database Design Dept. of Computer Science Vassar College
376a. Database Design Dept. of Computer Science Vassar College
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Presentation transcript:

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

NOEA/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

NOEA/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

NOEA/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

NOEA/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

NOEA/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

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

NOEA/IT - FEN: Databases/SQL8 SQL2 - DML SELECT UPDATE INSERT DELETE

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

NOEA/IT - FEN: Databases/SQL10 The Company Database

NOEA/IT - FEN: Databases/SQL11 Company on SQL Server Script:

NOEA/IT - FEN: Databases/SQL12 Relational Algebra - Overview

NOEA/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 * ----

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

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

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

NOEA/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

NOEA/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)

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

NOEA/IT - FEN: Databases/SQL20 SQL2 - DML Inserting a single row: INSERT INTOEMPLOYEE VALUES(’Richard’,’K’,’Marini’,’ ’, ’30-DEC-52’,’98 Oak Forest, Katy, ’TX’,’M’,37000,’ ’,4) Inserting a single row, only selected attributes: INSERT INTOEMPLOYEE(FNAME,LNAME,SSN) VALUES(’Richard’,’Marini’,’ ’) Is rejected if any of the other attributes is defined NOT NULL and doesn’t have defined a default value.

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

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

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

NOEA/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