SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

Module 5: Implementing Data Integrity. Overview Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints.
Fundamentals, Design, and Implementation, 9/e Chapter 5 Database Design.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
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.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
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.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
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.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
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 Server 7.0 Maintaining Referential Integrity.
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.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL: DDL John Ortiz Cs.utsa.edu.
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.
© 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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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.
Module 4: Implementing Data Integrity
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.
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.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
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.
Databases Introduction - concepts. Concepts of Relational Databases.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
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 ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Getting started with Accurately Storing Data
Fundamental of Database Systems
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Module 5: Implementing Data Integrity by Using Constraints
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL data definition using Oracle
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
CS122 Using Relational Databases and SQL
Database Management System
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Instructor: Samia arshad
CS122 Using Relational Databases and SQL
Including Constraints
Presentation transcript:

SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints

Different types of constraints Entity constraints – PRIMARY KEY – UNIQUE Domain constraints – CHECK constraints CHECK salary > 0 – DEFAULT value Referential integrity constraints – FOREIGN KEY Column level – Constrains applies to a column Table level – Constrains applies to a table 2SQL DDL Constraints

Checking constraints Constraints are automatically checked by the DBMS every time you try to do – INSERT, – UPDATE – or DELETE SQL DDL Constraints3

Constraints should have a name Constraints (like other database objects) have names. Names are use when constraints are created, altered or dropped. Example names – PK_Student_ID Primary key in the Student table, is ID Default names – SQL Server will generate default names for your constrains Which can be quite unreadable – You might be better of naming the constraints your self SQL DDL Constraints4

Primary key constraint Every table should have a primary key. A primary key is a set of attributes – Often the set has only 1 element The values of the primary key attributes must be unique. Primary key attributes must be NOT NULL Primary keys should generally be ID – Don’t use real data as a primary key – Data type INT. Generated using IDENTITY(1,1) Syntax – Attrib dataType IDENTITY(1,1) PRIMARY KEY 5SQL DDL Constraints

UNIQUE constrains Sometimes a table has more candidate keys – One candidate is selected PRIMARY KEY – Others are declared UNIQUE Syntax – Attrib DataType UNIQUE [NOT NULL] – Unlike PRIMARY KEY a UNIQUE attribute can accept NULL 6SQL DDL Constraints

DEFAULT constraints Default values Used in INSERT statements when no value is supplied Example – enrollmentDate data DEFAULT getDate() 7SQL DDL Constraints

Check constraints Simple business rules Examples – Salary int CHECK (salary > 0) – Month tinyInt CHECK (month BETWEEN 1 AND 12) – Day varchar(10) CHECK (day IN (’Monday’, ’Tuesday’, et.) 8SQL DDL Constraints

Foreign key constraints Dependency between two tables – Referring table Has the foreign key – Referenced table The foreign key reefers to the primary key of this table – Recursive relationship / self referencing table The referring table and the referenced table is the same Examples – Employee has a supervisor / boss, who is another Employee – Category has a super category Syntax – Attrib dataType FOREIGN KEY REFERENCES tableName (attributName) 9SQL DDL Constraints

Cascading actions Generally you cannot update / delete referred rows, but … Syntax – CONSTRAINT someName FOREIGN KEY REFERENCES sometable(someattribute_s) ON UPDATE someActionA ON DELETE someActionB – someAction can be No action: default Cascade: referring rows are updated / deleted Set null: referring values are set to null Set default: referring value are set to their default value SQL DDL Constraints10

Ignoring existing data when you create a constraint Adding a constraint to an existing table can be a problem – If the table has data that does NOT conform with the constraint – It is possible to add a constraint WITHOUT checking the existing rows ALTER TABLE … WITH NOCHECK ADD CONSTRAINT … 11SQL DDL Constraints

Disabling and enabling constraints checking ALTER TABLE … NOCHECK CONSTRAINT constraintName – Checking is disabled ALTER TABLE … CHECK CONSTRAINT constraintName – Checking is enabled 12SQL DDL Constraints