Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.

Slides:



Advertisements
Similar presentations
1 DBS201: More on SQL Lecture 3. 2 Agenda Review Constraints Primary Key Not Null Unique Check Foreign Key Changing a Table Definition Changing and deleting.
Advertisements

Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, 4th Edition, Pratt & Adamski
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Data Integrity Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Oracle Data Definition Language (DDL)
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Recap of SQL Lab no 8 Advance Database Management System.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Week 5 Lecture 2 Data Integrity Constraints. Learning Objectives  Learn the types and the uses of constraints  Examine the syntax and options for creating.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
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.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Chapter 6: Integrity (and Security)
Insert, Update and the rest…
Constraints and Triggers
SQL Creating and Managing Tables
SQL: Constraints and Triggers
Referential Integrity
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Creating and Managing Tables
DBS201: More on SQL Lecture 3
Referential Integrity
SQL DATA CONSTRAINTS.
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Including Constraints
DBS201: More on SQL Lecture 3
Presentation transcript:

Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007

2 AGENDA: Constraining the Values of Data Maintaining Referential Integrity Entering, Deleting, and Changing Data Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : 4 – 6

3 Constraining the Values of Data Declaring constraints – Values that can be entered into its columns on a table – SQL will generally reject any values that violate the criteria you created before Syntax: CREATE TABLE tablename ({columnname datatype column constraint} …, [table constraint (columnname,…),…]; Alternative : ALTER TABLE

4 Constraining the Values of Data (cont’d) Using Constraints to Exclude NULLs – To prevent a column from permitting NULLs as a value – Use CREATE TABLE statement with NOT NULLs constraint Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL, sname CHAR(10) NOT NULL, city CHAR(10), comm DECIMAL); Column with NOT NULL constraint must be assigned values in every INSERT clause.

5 Constraining the Values of Data (cont’d) Specifying the Primary Key Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL, city CHAR(10), comm DECIMAL); Primary Keys of More Than One Column Example: CREATE TABLE Namefield (firstname CHAR(10) NOT NULL, lastname CHAR(10) NOT NULL, city CHAR(10), PRIMARY KEY (firstname, lastname));

6 Constraining the Values of Data (cont’d) Making Sure Values Are Unique – Using UNIQUE constraint – UNIQUE vs. PRIMARY KEY: PRIMARY KEY constraint can only be used on one column or group of columns. UNIQUE constraint may be used any number of times. PRIMARY KEY column may not contain NULLs, UNIQUE constraint may. – Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL UNIQUE, city CHAR(10), comm DECIMAL);

7 Constraining the Values of Data (cont’d) Checking Column Values – Using CHECK to Predetermine Valid Input Values – Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL, city CHAR(10) CHECK, (city IN (‘London’, ‘New York’, ‘San Jose’, ‘Barcelona’)), comm DECIMAL CHECK (comm < 1)); – Check Conditions Based on Multiple Columns – Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL UNIQUE, city CHAR(10), comm DECIMAL, CHECK (comm <.15 OR city = ‘Barcelona’));

8 Constraining the Values of Data (cont’d) Naming and Dropping Constraints Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL UNIQUE, city CHAR(10), comm DECIMAL, CONSTRAINT LuckyBarcelona CHECK (comm <.15 OR city = ‘Barcelona’)); Dropping constraints: ALTER TABLE Salespeople DROP CONSTRAINT LuckyBarcelona;

9 Constraining the Values of Data (cont’d) Assigning Default Values Example: CREATE TABLE Salespeople (snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL, city CHAR(10) DEFAULT = ‘New York’, comm DECIMAL CHECK (comm < 1)); Default Values and NULLs

10 Maintaining Referential Integrity Foreign Key and Parent Key Declaring Columns As Foreign Key: 1. FOREIGN KEY as a table constraint CREATE TABLE Customers (cnum INTEGER NOT NULL PRIMARY KEY, cname CHAR(10), city CHAR(10), snum INTEGER, FOREIGN KEY (snum) REFERENCES Salespeople(snum)); Note: Customers table with snum defined as a foreign key referencing the Salespeople table.

11 Maintaining Referential Integrity (cont’d) 2. FOREIGN KEY as a column constraint CREATE TABLE Customers (cnum INTEGER NOT NULL PRIMARY KEY, cname CHAR(10), city CHAR(10), snum INTEGER REFERENCES Salespeople(snum)); Note: Customers table with snum as a foreign key whose parent key is Salespeople.snum. It is equivalent to this table constraint: FOREIGN KEY (snum) REFERENCES Salespeople (snum)

12 Maintaining Referential Integrity (cont’d) 3. Using FOREIGN KEY to predetermined valid input values CREATE TABLE Officecities (office (CHAR(10) NOT NULL PRIMARY KEY); CREATE TABLE Salespeople ( snum INTEGER NOT NULL PRIMARY KEY, sname CHAR(10) NOT NULL, city CHAR(10) REFERENCES Officecities, comm DECIMAL CHECK (comm < 1));

13 Maintaining Referential Integrity (cont’d) PRIMARY vs. UNIQUE Parent Keys Multicolumn Foreign Keys Referential Triggered Actions : – how parent-key changes affect foreign keys – It enable you to specify when creating a FOREIGN KEY constraint what you would like to have happen to the foreign-key values when the referenced parent-key values are deleted or changed. – UPDATE : what happens to a foreign key value when the parent key value it references is changed. – DELETE : what happens to a foreign key value when the parent key row it references is deleted. – Solutions : CASCADE, SET NULL, SET DEFAULT, NO ACTION

14 Entering, Deleting, and Changing Data The INSERT statement Entering values into tables Syntax: INSERT INTO table name VALUES (value,…); Example: INSERT INTO Salespeople VALUES (1001, ‘Peel’, ‘London’,.12); Inserting NULLs value INSERT INTO Salespeople VALUES (1001, ‘Peel’, NULL,.12);

15 Entering, Deleting, and Changing Data (cont’d) Naming columns for INSERT INSERT INTO Customers (city, cname, cnum) VALUES (‘London’, ’Hoffman’, 2001); Inserting the Result of a Query INSERT INTO Londonstaff SELECT * FROM Salespeople WHERE city = ‘London’; INSERT INTO Londonstaff SELECT snum, sname, city, comm * 1.1 FROM Salespeople WHERE city = ‘London’;

16 Entering, Deleting, and Changing Data (cont’d) The DELETE statement Removing Rows from tables DELETE FROM Salespeople; DELETE FROM Salespeople WHERE snum = 1003; DELETE FROM Salespeople WHERE city = ‘London’;

17 Entering, Deleting, and Changing Data (cont’d) The UPDATE Statement Changing field values Example: UPDATE Customers SET rating = 200; Updating only certain rows UPDATE Customers SET rating = 200 WHERE snum = 1001;

18 Entering, Deleting, and Changing Data (cont’d) Updating multiple columns at once UPDATE Salespeople SET sname = ‘Gibson’, city = ‘Boston’, comm =.10 WHERE snum = 1004; Using value expression in UPDATE UPDATE Salespeople SET comm = comm * 2 WHERE city = ‘London’; Updating to NULL value UPDATE Customers SET rating = NULL Where city = ‘London’;

19 End of Maintaining Referential Integrity Thank you