More SQL Data Management 2005. Topics zIntegrity Constraints zOuter Join zUnion.

Slides:



Advertisements
Similar presentations
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Advertisements

BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
6.830/6.814 Lecture 5 Database Internals Continued September 17, 2014.
Data Management zChris Wallace y3P14 zBook yVarious Database texts yLast year Database Systems Paul Beynon-Davies £24.99 zExam.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Bogdan Shishedjiev SQL1 SQL Reminder of SQL. Bogdan Shishedjiev SQL 2 Subsets of language Data definition language (DDL) –Domain definition –Schema definition.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
ISD3 Lecture 4 - Databases, SQL and MySQL. dept deptno dname location emp empno ename not null job not null hiredate sal comm manager The EMP DEPT database.
SQL Components DML DDL DAL. Overview u Getting the records onto the disk - mapping u Managing disk space u SQL Modes u Ceating database.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Oracle Data Definition Language (DDL)
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL (DDL & DML Commands)
Unit 6 Data Storage Design. Key Concepts 1. Database overview 2. SQL review 3. Designing fields 4. Denormalization 5. File organization 6. Object-relational.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
1 Lecture 4 Data Integrity: Primary and Foreign Keys.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
COP-5725 Practice Exercises
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Inner Joins These slides are licensed.
11-1 © Prentice Hall, 2004 Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
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.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
Including Constraints
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Database Systems Instructor Name: Lecture-12.
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
Writing Correlated Subqueries
JOINS.
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
(SQL) Manipulating Data
Database Design: Relational Model
Copyright © Ellis Cohen
Presentation transcript:

More SQL Data Management 2005

Topics zIntegrity Constraints zOuter Join zUnion

Types of ‘Integrity’ Constraints zStatic zTrue for all states zState invariant zTrue at all times ze.g. for all e in emp, e.dept in (dept.deptno) z Transition z Between two states of the system S,S’ z Conditions that must apply before a change, and then apply after z pre- and post-conditions z e.g. e’.sal > e.sal i.e. any wage change must be upwards!

Types of Database Function zQuery zExtracts existing facts and derives new facts zLeaves Factbase unchanged zSELECT … z Update z Reflects some change in the UoD z Changes state of factbase z Must check integrity constraints z UPDATE.., INSERT.., DELETE..

DDL zCREATE TABLE ydefine the table name ydefine the fields of the table ydefine constraints on the table zMODIFY TABLE yadd a field or constraint zDROP TABLE ydelete the table

DDL commands zCREATE TABLE EMP ( Empno char(8) not null, … Constraint emp_pk primary key(empno) ); zALTER TABLE EMP yADD SPOUSE VARCHAR(40) ); zDROP TABLE EMP;

Integrity Constraints and Business Rules zDatabase integrity yRules which ensure that relationships are well- defined zBusiness Rules yAdditional constraints on values and interrelationships

Data integrity zPrimary key xCONSTRAINT emp_pk PRIMARY KEY (empno) zNot Null xEname NOT NULL zField Validation xCONSTRAINT emp_sal CHECK (sal <6000) xCONSTRAINT emp_job CHECK (job IN (‘analyst,’salesman’, ‘manager’, ‘president’, ‘clerk’))

Foreign Key zRelationship implentation y‘You take the primary key of the one side and add it to the many side” yBut there is no reference to which table the key came from! zForeign key constraint xCONSTRAINT emp-deptfk FOREIGN KEY (deptno) REFERENCES dept NO ACTION – reject attempt to delete parent if children exist CASCADE – delete all the child records if the parent is deleted SET NULL – set foreign key field to null SET DEFAULT – set foreign key field to its default

dept deptno dname location emp empno ename not null job not null hiredate sal comm manager The EMP DEPT database

Coding constraints Exercise zCode up all the constraints for the emp- dept database

Outer Join zInner join returns only matching rows zWe can get non-matching rows too yA LEFT JOIN B xincludes all the non matching rows in A as well yA RIGHT JOIN B xincludes all the non matching rows in B as well ya FULL JOIN B xincludes non matching rows from both tables

yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M,F y23 fred23 sue y23 fred30 alice y23 fred34 julie y20 joe23 sue y20 joe30 alice y20 joe34 julie y34 bill23 sue y34 bill30 alice y34 bill 34 julie y

yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M inner join F on M.age = F.age y23 fred23 sue y23 fred30 alice y23 fred34 julie y20 joe23 sue y20 joe30 alice y20 joe34 julie y34 bill23 sue y34 bill30 alice y34 bill 34 julie y

yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M left join on M.age = F.age y23 fred23 sue y34 bill 34 julie y

yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M right join on M.age = F.age y23 fred23 sue y34 bill 34 julie y

yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M full join on M.age = F.age y23 fred23 sue y34 bill 34 julie y

yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M, F on M.age > F.age y23 fred23 sue y23 fred30 alice y23 fred34 julie y20 joe23 sue y20 joe30 alice y20 joe34 julie y34 bill23 sue y34 bill30 alice y34 bill 34 julie y

zInner Join (natural Join) xSelect * xfrom M inner join F on M.age=F.age; zLeft (Outer) Join xSelect * xfrom M left join F on M.age=F.age; zRight (Outer) Join xSelect * xfrom M right join F on M.age=F.age; zFull (outer) Join xSelect * xfrom M full join F on M.age=F.age;

zShow all departments without employees ySelect dname yfrom dept left join emp yon dept.deptno = emp.deptno xshows all the department names ywhere ename IS NULL; xshows only the departments without employees

Union zWe can Union two or more tables in the one statement zHandy if the separate select statements use different tables zwhat does this query do? Select empno, ename, job from emp where job='Manager' union Select m.empno, m.ename, m.job from emp e, emp m where m.empno = e.mgr;