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.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
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.
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
1 DBS201: More on SQL Lecture 2. 2 Agenda Review How to create a table How to insert data into a table Terms Lab 2.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Visual Programing SQL Overview Section 1.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
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.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Chapter 3: Relational Databases
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamentals of DBMS Notes-1.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
DBS201: More on SQL Lecture 2.
SQL Creating and Managing Tables
DBS201: More on SQL Lecture 3
SQL data definition using Oracle
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
Database Management System
Data Definition Language
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
DBS201: More on SQL Lecture 2.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Including Constraints
DBS201: More on SQL Lecture 3
Presentation transcript:

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 Table Rows

3 Review What information is stored in a database structure chart? Data Dictionary shows Column Name Column Type Column Length PK? FK Reference? Req’d? Unique? Validation?

4 Review Terms starting with the largest SQL Terms Native Equivalents Schema/CollectionLibrary TablePhysical File RowRecord ColumnField ViewLogical File IndexLogical File

5 Review CREATE TABLE painter ( p_num char (4) not null with default, p_lname char (15) not null with default, p_fname char (15) not null with default, p_city char (20), p_phone dec (10), Constraint Painter_P_num_PK primary key (p_num) ) Table PAINTER in DR201C40 created but was not journaled.

6 Review Is different than

7 Review CREATE TABLE painter ( p_num char (4) not null with default, p_lname char (15) not null with default, p_fname char (15) not null with default, p_city char (20), p_phone dec (10), Constraint Painter_P_num_PK primary key (p_num) ) Table PAINTER created in PAINTRUS

8 Constraints  Constraints are used to ensure that the database receives “good” data PRIMARY KEY  unique and not null. NOT NULL  a value must exist. UNIQUE  value exists only once in a column. CHECK  value must pass validation criteria FOREIGN KEY  used to enforce Relational Integrity between two tables

9 Constraints – Primary Key  PRIMARY KEY  Provides rapid access to a row on a table  Guarantees uniqueness of the PK field (there can be only one Doctor # D5524 in Ontario)  Insists that a primary key value be specified by the user for every row on the table.

10 Constraints – Not Null  NOT NULL  The user must specify a value.  NOT NULL WITH DEFAULT  The user must specify a value or the system will specify a value.(spaces for char, 0 for numeric and “sysdate” for date fields)  NOT NULL WITH DEFAULT “value”  The user must specify a value or the system will insert “value” into the field.

11 Constraints – Unique  UNIQUE  The database will not allow rows to be added or modified if it results in the unique field occurring more than once in the column in a table.  This is useful for fields like:  Social Insurance Number  Driver’s License Number  Ontario Health Card Number  Why not make these a primary key to uniquely locate a table row?  Do you want someone in your company having any of these unique numbers when they need to update your address?

12 Constraints – Check  CHECK  The database will examine the value of the field. There is a condition to be satisfied. The database will not allow rows to be added or modified if it results in the field failing the validation.  Here are some common validations:  (Age >= 18 and Age <= 49)  (Salary BETWEEN and 40000)  (Grade IN(‘A’,’B’,’C’,’D’,’F’,’I’))

13 Constraints – Check  CREATE TABLE TEST (  AGE DECIMAL (3,0) NOT NULL,  SALARY DECIMAL (7,2) NOT NULL WITH DEFAULT,  GRADE CHARACTER (1),  CONSTRAINT TEST_AGE_CK  CHECK (AGE >= 18 AND AGE <= 49),  CONSTRAINT TEST_SALARY_CK  CHECK (SALARY BETWEEN AND 4000),  CONSTRAINT TEST_GRADE_CK  CHECK (GRADE IN('A', 'B', 'C', 'D', 'F', 'I') ) )

14 Constraints – Foreign Key FOREIGN KEY The Foreign Key is used to get information from another table using the Primary Key of the other table. A PATIENT record has the DoctorNum as a Foreign Key field. DoctorNum is the Primary Key of the DOCTOR table. The DoctorNum is used to get information about the Doctor from the DOCTOR table. The Foreign Key is used to enforce relational integrity between the two tables. We will have more to say about Relational Integrity later in this course.

15 Constraints - Examples  CREATE TABLE customer(  c_num char (5) not null with default,  c_lname char (15) not null with default,  c_fname char (15) not null with default,  c_city char (20),  c_phone dec (10),  cp_num char(4),  Constraint Customer_c_num_PK  primary key (c_num),  Constraint Customer_Customer_cp_num_FK  Foreign Key (cp_num)  References paintrus.Painter (p_num ) )

16 Foreign Key Constraint INSERT INTO PAINTRUS/CUSTOMER VALUES('99999', 'Smith', 'Ronald', 'Mississauga', , '111') Operation not allowed by referential constraint CUSTOMER_CUSTOMER_CP INSERT INTO PAINTRUS/painter VALUES('111', 'Wong', 'Mary','Mississauga', ) 1 rows inserted in PAINTER in PAINTRUS. INSERT INTO PAINTRUS/CUSTOMER VALUES('99999', 'Smith', 'Ronald', 'Mississauga', , '111') 1 rows inserted in CUSTOMER in PAINTRUS.

17 Foreign Key Constraint delete from painter Delete prevented by referential constraint CUSTOMER_CUSTOMER_CP_NUM delete from customer 1 rows deleted from CUSTOMER in PAINTRUS. delete from painter 1 rows deleted from PAINTER in PAINTRUS

18 Constraints - Examples  LAB 3 has examples of these 5 types of constraints.

19 Agenda Data Definition Language How to use SQL to update table definitions Data Manipulation Language How to update data in a table How to delete rows of data in a table

20 Changing a Table Definition ALTER TABLE Used to update a database definition Syntax ALTER TABLE tablename Can do any of the following Add a field Alter a field Drop a field Add a constraint Drop a constraint

21 Changing a Table Definition To add a field: ALTER TABLE tablename ADD COLUMN field-name datatype Example: ALTER TABLE MARINA ADD COLUMN Boat_Description CHAR (20)

22 Changing a Table Definition To change a field definition: ALTER TABLE tablename ALTER COLUMN fieldname SET DATA TYPE data type Example: ALTER TABLE MARINA ALTER COLUMN Boat_Description SET DATA TYPE CHAR(12) NOT NULL WITH DEFAULT 'abc'

23 Changing a Table Definition To remove a field: ALTER TABLE tablename DROP COLUMN column_name Example: ALTER TABLE MARINA DROP COLUMN Boat_Description *Note – be careful not to drop a column that you may in fact need as the data will be lost. Rule of thumb: do not alter a table after it contains data

24 Changing a Table Definition To add a constraint: Example defining the PRIMARY KEY: ALTER TABLE MARINA ADD CONSTRAINT Marina_Boat_No_PK PRIMARY KEY (Boat_No) Example of adding a check constraint: ALTER TABLE MARINA ADD CONSTRAINT Marina_Boat_Code_CK CHECK (Boat_Code BETWEEN 'AAA' AND 'DDD‘)

25 Updating a Table Definition - DDL To drop a constraint ALTER TABLE tablename DROP CONSTRAINT constraint_name ALTER TABLE MARINA DROP CONSTRAINT Boat_Name_UN Name must be the name assigned to the constraint (may have been assigned by user or by system)

26 Updating Data in a Table To update data in a table, use UPDATE. UPDATE tablename SET column name = new value WHERE condition examples UPDATE MARINA SET Dock_Number = ‘AAA' UPDATE MARINA SET Dock_Number = ‘AAB‘ WHERE Marina_Num = ‘M-2407’

27 How to Delete Rows in Tables-DML To delete a row in a table, Use the Delete statement DELETE FROM tablename WHERE condition example DELETE FROM MARINA WHERE Dock_Num = ‘CCC’

28 Updating a Table Definition - DDL Adding a Primayr Key and Foreign Key Constraint ALTER TABLE CUSTOMER ADD CONSTRAINT CUSTOMER_CUSTOMER_NUMBER_PK PRIMARY KEY (CUSTOMER_NUMBER) ALTER TABLE CUSTOMER ADD CONSTRAINT CUSTOMER_SALES_REP_NUMBER_FK FOREIGN KEY (SALES_REP_NUMBER) REFERENCES SALESREP(SALES_REP_NUMBER) Table SALESREP in PRMIERC40 does not have a matching parent key