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

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.
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.
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Database technology Lecture 2: Relational databases and SQL
Data Definition Languages Atif Farid Mohammad UNCC.
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
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.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
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.
FEN  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
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.
CS 380 Introduction to Database Systems (Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views)
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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.
莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
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.
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.
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.
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 –continue with SQL (chapter.
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
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
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.
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) هي وسيلة لتوضيح الدرس واداة.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
SQL: SchemaDefinition, Constraints, and Queries and Views
376a. Database Design Dept. of Computer Science Vassar College
Company Requirements.
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL-99: Schema Definition, Constraints, and Queries and Views
Session - 6 Sequence - 5 SQL Updating Database Contents
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
Presentation transcript:

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

Example: Company - Schema FEN

Example: Company - Sample Data FEN

Example: Company - Foreign Key Constraints FEN

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

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

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 doesn’t 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