Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.

Slides:



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

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
1 The Relational Model Instructor: Mohamed Eltabakh
Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
SQL: DDL John Ortiz Cs.utsa.edu.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
1 Dept. of CIS, Temple Univ. CIS616/661 – Principles of Data Management V. Megalooikonomou SQL (part 2) (based on slides by C. Faloutsos at CMU)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 480: Database Systems Lecture 13 February 13,2013.
Introduction to Database Systems, CS420
Database Management Systems (CS 564)
Instructor: Mohamed Eltabakh
The Relational Model Relational Data Model
SQL OVERVIEW DEFINING A SCHEMA
SQL.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Instructor: Mohamed Eltabakh
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS Notes 08 2 SQL -- historical Perspective Developed by IBM in mid 70s First standardized in 1986 by ANSI --- SQL1 Revised in SQL2. Approximate 580 pages describing syntax and semantics In 1999, ANSI/ISO released the SQL3. Many additions for: –support for multimedia data –addition of abstract data types and object-orientation –support for calling programmed functions from within SQL Every vendor has a slightly different version of SQL If you ignore the details, basic SQL is very simple and declarative. Hence easy to use.

Fall 2005 ICS184/EECS Notes 08 3 SQL in Different Roles Data definition language (DDL): –allows users to describe the relations and constraints. Constraint specification language: –commands to specify constraints ensured by DBMS Query language: –relationally complete, supports aggregation and grouping –declarative -- you specify what you want and not how to retrieve, easy to learn and program Updates: –insert, delete, and update tables View definition language: –commands to define rules –updates through views generally not supported

Fall 2005 ICS184/EECS Notes 08 4 SQL in Different Roles (cont) Embedded SQL: –has been embedded in a variety of host languages:  C, C++, PERL, Smalltalk, Java (vendor dependent) –Impedance mismatch: SQL manipulates relations that are sets/bags --- programming languages do not handle sets/bags as efficiently. Transaction Control: –commands to specify beginning and end of transactions.

Fall 2005 ICS184/EECS Notes 08 5 SQL as DDL CREATE TABLE Dept (dnoint, dnamevarchar(30) not null, mgrchar(15) ); CREATE TABLE emp (enamechar(15)not null, dnoint default 0, salint ); Don’t allow null values Default value is 0

Fall 2005 ICS184/EECS Notes 08 6 Domain types char(n): fixed length char string varchar(n): variable-length char string with at most n chars int or integer smallint numeric(p,d): fixed-point number of given precision real, double precision float(n): floats with a given precision date: containing year,month, and date – yyyy-month-day time: in hours, minutes, and seconds – hour:min:sec Null value is part of each domain

Fall 2005 ICS184/EECS Notes 08 7 Define new domains CREAT DOMAIN personDom CHAR(20); CREATE TABLE emp (enamepersonDom, dnoint default 0, salreal );

Fall 2005 ICS184/EECS Notes 08 8 Schema Definition CREATE TABLE r ( A1D1 [not null] [default V1] … AnDn [not null] [default Vn] … ) Each integrity constraints could be: –primary key –candidate key –foreign key –“check(predicate)”: specifies predicate to be satisfied by each tuple

Fall 2005 ICS184/EECS Notes 08 9 Declaring Keys CREATE TABLE emp ( ssnint Primary Key, namechar(15), dnoint, ); PRIMARY KEY or UNIQUE CREATE TABLE emp ( ssnint, namechar(15), dnoint, Primary Key (ssn) ); CREATE TABLE emp ( ssnint, namechar(15), dnoint, Primary Key (dno,name) ); Multiple attributes

Fall 2005 ICS184/EECS Notes Comparison A table has only one “primary key” in a table, but many “uniques” Primary keys cannot have NULLs “Unique” may have NULLs. –Two different tuples cannot have the same nonnull values in the “unique” attributes. CREATE TABLE emp ( ssnint, namechar(15), dnoint, Unique (dno,name) ); CREATE TABLE emp ( ssnint UNIQUE, namechar(15), dnoint, ); eName: UNIQUE

Fall 2005 ICS184/EECS Notes Enforcing Key Constraints Check constraint each time the table is modified –Insertion: check –Update: check –Deletion: do not check Enforcing key constraints efficiently: –Suppose “ssn” is a primary of Emp. –Every time we insert a new employee, do we want to scan the whole table to check if the ssn already exists? No! –Using index on the key attribute(s)  Advanced topic -- ignored

Fall 2005 ICS184/EECS Notes Foreign-Key Examples CREATE TABLE emp ( ssn int, name char(15), dno int REFERENCES dept(dno), ); CREATE TABLE emp ( ssn int, name char(15), dno int, FOREIGN KEY dno REFERENCES dept(dno)); Allow multiple attributes in one foreign-key constraint. Allow multiple foreign-key constraints in one table. Emp (ename, dno, sal) Dept(dno, dname, mgr)

Fall 2005 ICS184/EECS Notes Foreign-key Constraints Also called “referential integrity” Within an attribute: –REFERENCES ( ) Separate declaration: –FOREIGN KEY REFERENCES ( ) If R.A references S.B, then S.B must be a primary key. NULL values allowed for attributes in a foreign key. –A foreign-key constraint is automatically satisfied if even one attribute in the foreign key is null.

Fall 2005 ICS184/EECS Notes Enforcing Foreign-Key Constraints Modification (insert, update) of Emp –If the new tuple’s dno does not exist in Dept.dno, then REJECT! Emp.dno references Dept.dno Emp (ename, dno, sal) Dept(dno, dname, mgr)

Fall 2005 ICS184/EECS Notes Enforcing Foreign-Key Constraints Emp.dno references Dept.dno Emp (ename, dno, sal) Dept(dno, dname, mgr) Modification (delete, update) of Dept whose old “dno” is referenced by a record in Emp. There are 3 strategies: –Default: reject –Cascade: change the Emp tuple correspondingly  Delete in Dept: delete the referring record(s) in Emp  Update in Dept: update the dno of the referring record(s) in Emp to the new dno –Set Null: change dno value in referring record(s) in Emp to NULL

Fall 2005 ICS184/EECS Notes Choosing a Policy Add “ ON [DELETE,UPDATE] [CASCADE, SET NULL] ” when declaring a foreign key Which policy to choose depends on the application. CREATE TABLE emp ( ssn int, name char(15), dno int, FOREIGN KEY dno REFERENCES dept(dno) ON DELETE SET NULL ON UPDATE CASCADE );

Fall 2005 ICS184/EECS Notes Attribute-based Checks CREATE TABLE Emp ( name CHAR(30), dno int, gender CHAR(1) CHECK (gender in (‘F’, ‘M’)), age int CHECK (age > 18 AND age < 100) ); Constraints on attribute values. Checked when there is an insertion or update of the attribute.

Fall 2005 ICS184/EECS Notes Attribute-based Checks (cont) Syntax: CHECK (condition) Condition may involve the checked attribute Other attributes and relations may be used (in a query) Condition checked only when that associated attribute changes CREATE TABLE Emp ( ssn int, name CHAR(30), dno int CHECK (dno in (SELECT dno from dept)) ); –Condition checked when we insert/update Emp, but NOT when we modify dept.

Fall 2005 ICS184/EECS Notes Tuple-Based Checks CREATE TABLE Emp ( ssn int, gender CHAR(1), age int, dno int, CHECK (gender in (‘F’, ‘M’)), CHECK (age > 18 AND age < 100) ); Checked whenever a tuple is inserted or updated Again, other relations may be used.

Fall 2005 ICS184/EECS Notes Tuple-Based Checks (cont) CREATE TABLE Emp ( ssn int, gender CHAR(1), age int, dno int, CHECK (dno in (SELECT dno from dept)) ); If someone inserts an employee whose dno does not exist in Dept.dno, the insertion will be rejected. However, if we delete a record from Dept whose dno is used by an employee tuple, it will NOT be rejected. Emp (ename, dno, sal) Dept(dno, dname, mgr)

Fall 2005 ICS184/EECS Notes Schema Modification Delete a relation R: DROP TABLE R; Modify a relation: –Add new columns ALTER TABLE emp ADD address CHAR(20); –Delete existing columns ALTER TABLE emp DROP birthday;

Fall 2005 ICS184/EECS Notes Database Modifications Insert Delete Update

Fall 2005 ICS184/EECS Notes Insertion of a tuple INSERT INTO R(A1,…,An) VALUES (v1,…,vn) Example: INSERT INTO Emp (ename, dno, sal) VALUES (’Tom’, 123, 45000) Can drop attribute names if we provide all of them in order. INSERT INTO Emp VALUES (’Tom’, 123, 45000) If we don’t provide all attributes, they will be filled with NULL. INSERT INTO Emp (ename,sal) VALUES (‘Tom’, 45000)

Fall 2005 ICS184/EECS Notes Delete DELETE FROM relation [WHERE conditions]; Example: DELETE FROM emp WHERE dno = 123; DELETE FROM emp;  all tuples will be deleted There is no way to delete only a single occurrence of a tuple that appears twice in a relation.

Fall 2005 ICS184/EECS Notes Delete (cont) Delete all employees working in a department with only one employee. DELETE FROM emp AS E1 WHERE NOT EXISTS (SELECT ename FROM emp WHERE dno = E1.dno AND ename <> E1.ename); Note the relation renaming “E1”

Fall 2005 ICS184/EECS Notes Update UPDATE relation SET assignments WHERE condition “Change employees in dept 123 to dept 345.” UPDATE emp SET dno = 345 WHERE dno = 123; “Cut the salaries that are more than 100K by 10%.” UPDATE emp SET sal = sal * 0.9 WHERE sal > ; Multiple assignments separated by “,” UPDATE emp SET dno = 345, sal = sal * 1.1 WHERE dno = 123;