1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.

Slides:



Advertisements
Similar presentations
Alter an existing Table Note: (a) Primary key: SID + Course_code.
Advertisements

SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Sections 10 – Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
INTEGRITY Enforcing integrity in Oracle. Oracle Tables mrobbert owner granted access.
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)
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
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.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Programming Sections 9 – Constraints. Marge Hohly2 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
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.
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.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
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.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Session 1 Module 1: Introduction to Data Integrity
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Database Programming Sections 9 & 10 – DDL Data Definition Language,
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
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.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
In this session, you will learn to: Manage databases Manage tables Objectives.
More SQL: Complex Queries,
Including Constraints
Constraints and Triggers
SQL Creating and Managing Tables
Module 5: Implementing Data Integrity by Using Constraints
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Creating and Managing Tables
SQL data definition using Oracle
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
SQL DATA CONSTRAINTS.
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
CS122 Using Relational Databases and SQL
Instructor: Samia arshad
Including Constraints
Presentation transcript:

1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints Assign user defined names to constraints The Unique Key constraints –Business Rule constraint Column level constraints Table level constraints NULL value concepts NOT NULL constraints defined at the column level The CHECK constraints Defining different constraints on a table The user_constraints table Defining the integrity constraints via the ALTER TABLE command Dropping the integrity constraints via the ALTER TABLE command Default value concepts

2 Data Constraints Business rules which are enforced on data being stored in a table are called Constraints. –Applying data constraints CREATE TABLE, ALTER TABLE – attach constraints to a table. Types of data constraints I/O Constraints, Business Rule constraint. –I/O constraints The PRIMARY KEY constraints Can be defined by CREATE TABLE, ALTER TABLE statement PRIMARY KEY constraints defined at column level ( ) PRIMARY KEY PRIMARY KEY constraints defined at table level PRIMARY KEY (, ) The FOREIGN KEY constraints It represents relationships between tables. FOREIGN KEY constraints defined at the column level ( ) REFERENCES [( )] [ON DELETE CASCADE] FOREIGN KEY constraints defined at the table level FOREIGN KEY ( ) REFERENCES [(, )]

3 Assign user defined names to constraints CONSTRAINT eg. “emp_code” varchar(5) CONSTRAINTS p_empCode PRIMARY KEY CONSTRAINTS f_dept FOREIGN KEY dept REFERENCES ….. The Unique Key constraints The Unique column constraints permits multiple entries of NULL into column. UNIQUE constraints defined at the column level ( ) UNIQUE UNIQUE constraints defined at the table level CREATE TABLE tablename (, ) UNIQUE (, )); –Business Rule constraint CHECK Column level constraints Table level constraints NULL value concepts Operands such as =, cannot be used on a null value. IS NULL, IS NOT NULL conditions have to be used. NOT NULL constraints defined at the column level NOT NULL The CHECK constraints It must be specified as a logical expression that evaluates either to TRUE or FALSE. It takes longer to execute.

4 CHECK constraints defined at the column level CHECK ( ) CHECK constraints defined at the table level CHECK ( ) The user_constraints table DESCRIBE: column name, data types, size and not null. USER_CONSTRAINTS provides information bound to the names of all the constraints. (Owner, constraint_name, constraint_type, table_name, search_condition) eg. SELECT owner, constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE table_name = “xyz”; Defining the integrity constraints via the ALTER TABLE command Integrity constraints can be defined using the constraints clause in the ALTER TABLE command. eg. ALTER TABLE tablename ADD PRIMARY KEY (column1); ALTER TABLE table1 ADD CONSTRAINT f_key FOREIGN KEY (column1) REFERENCES table2 MODIFY(column2 NOT NULL); Dropping the integrity constraints via the ALTER TABLE command ALTER TABLE table1 DROP PRIMARY KEY; ALTER TABLE table1 DROP CONSTRAINT f_key;

5 Default value concepts DEFAULT ;