DBS201: More on SQL Lecture 3

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

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.
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.
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.
Introduction to Structured Query Language (SQL)
Database Management System LICT 3011 Eyad H. Elshami.
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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
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.
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.
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.
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.
Rob Gleasure robgleasure.com
Chapter 5 Introduction to SQL.
Rob Gleasure robgleasure.com
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
Constraints and Triggers
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
DBS201: More on SQL Lecture 2.
The Relational Model Relational Data Model
SQL Creating and Managing Tables
The Basics of Data Manipulation
SQL data definition using Oracle
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Database Management System
Relational Database Design
Data Definition Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
DBS201: More on SQL Lecture 2.
CS122 Using Relational Databases and SQL
IST 318 Database Administration
CS122 Using Relational Databases and SQL
Including Constraints
DBS201: More on SQL Lecture 3
Presentation transcript:

DBS201: More on SQL Lecture 3 Reference - Database Design and ANSI SQL by James Cooper Chapter 5, pages 97 - 107

Agenda Review Constraints Changing a Table Definition Primary Key Not Null Unique Check Foreign Key Changing a Table Definition Changing and deleting Table Rows

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?

Review Terms starting with the largest SQL Terms Native Equivalents Schema/Collection Library Table Physical File Row Record Column Field View Logical File Index Logical File

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.

Review Is different than

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

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

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

Constraints – Not Null NOT NULL NOT NULL WITH DEFAULT 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.

Constraints – Unique UNIQUE This is useful for fields like: 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?

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 20000 and 40000) (Grade IN(‘A’,’B’,’C’,’D’,’F’,’I’))

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 20000 AND 40000), CONSTRAINT TEST_GRADE_CK CHECK (GRADE IN('A', 'B', 'C', 'D', 'F', 'I') ) )

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

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 ) )

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

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. 1 rows deleted from PAINTER in PAINTRUS

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

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

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

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)

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'

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

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: ADD CONSTRAINT Marina_Boat_Code_CK CHECK (Boat_Code BETWEEN 'AAA' AND 'DDD‘)

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)

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' SET Dock_Number = ‘AAB‘ WHERE Marina_Num = ‘M-2407’

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’

Updating a Table Definition - DDL Adding a Primary Key and Foreign Key Constraint ALTER TABLE CUSTOMER ADD CONSTRAINT CUSTOMER_CUSTOMER_NUMBER_PK PRIMARY KEY (CUSTOMER_NUMBER) 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