At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.

Slides:



Advertisements
Similar presentations
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows in a table Delete rows from a table.
Sections 10 – Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Oracle Data Definition Language (DDL)
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
ORACLE SQL. Overview Personal DBMS Vs Client/Server DBMS Oracle 8 Environment SQL – syntax and examples PL/SQL-introduction.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
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.
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.
After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
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.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
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.
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.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
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.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
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.
Visual Programing SQL Overview Section 1.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
1 SQL Insert Update Delete Create table Alter table.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
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.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
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.
Including Constraints
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
SQL Statements SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME
ORACLE SQL.
Oracle Data Definition Language (DDL)
Database Management System
Including Constraints
Presentation transcript:

At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints

Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. The following constraint types are valid inMySQL: NOT NULL PRIMARY KEY REFERENCES Check

Constraints are named. Create a constraint: At the same time as the table is created After the table has been created Define a constraint at the column or table level. View a constraint in the data dictionary.

CREATE TABLE [schema.]table (columndatatype [DEFAULT expr] [column_constraint],... [table_constraint]); CREATE TABLE employee( (employee_nbr INTEGER(4), name VARCHAR(10),... dept_nbr DECIMAL(7,2) NOT NULL, PRIMARY KEY (employee_nbr));

Column constraint level Table constraint level column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type (column,...), column,... [CONSTRAINT constraint_name] constraint_type (column,...),

Ensures that null values are not permitted for the column Employee employee_nbr name job... commission dept_nbr 7839 King President Blake Manager Clark Manager Jones Manager20... NOT NULL constraint (no row may contain a null value for this column) Absence of NOT NULL constraint (any row can contain null for this column) NOT NULL constraint

Defined at the column level MySQL>CREATE TABLE employee( 2 employee_nbr INTEGER(4), 3name VARCHAR(10) NOT NULL, 4job VARCHAR(9), 5manager INTEGER(4), 6hire_date DATE, 7salary DECIMAL(7,2), 8 commission DECIMAL(7,2), 9dept_nbr DECIMAL(7,2) NOT NULL);

Department dept_nbrdept_name location 10 Accounting New York 20 ResearchDallas 30 Sales Chicago 40 Operations Boston PRIMARY key Insert into 20 Marketing Dallas Finance New York Not allowed (dept_nbr 20 already exists) Not allowed (dept_nbris null)

Defined at either the table level or the column level MySQL>CREATE TABLE dept( 2 dept_nbr NUMBER(2), 3dept_name VARCHAR2(14), 4location VARCHAR2(13), 5 PRIMARY KEY(dept_nbr));

Add or drop, but not modify, a constraint Enable or disable constraints Add a NOT NULL constraint by using the MODIFY clause ALTER TABLE table ADD [CONSTRAINT constraint] type (column); ALTER TABLE table ADD [CONSTRAINT constraint] type (column);

Add a PRIMARY KEY constraint to the EMP table indicating employee number is the primary key MySQL>ALTER TABLEemployee 2 ADD CONSTRAINT PRIMARY KEY(employee_nbr); Table altered.

Query the INFORMATION_SCHEMA.TABLE_CONSTRAINTS table view all constraint definitions and names. table_nameconstraint_nameconstraint_type category PRIMARY PRIMARY KEY department PRIMARY PRIMARY KEY employee emp_fk_manager FOREIGN KEY employee employee_fk_dept_nbr FOREIGN KEY employee PRIMARY PRIMARY KEY orders PRIMARY PRIMARY KEY product category_category_code_fk FOREIGN KEY product PRIMARY PRIMARY KEY salary_grade PRIMARY PRIMARY KEY table_nameconstraint_nameconstraint_type category PRIMARY PRIMARY KEY department PRIMARY PRIMARY KEY employee emp_fk_manager FOREIGN KEY employee employee_fk_dept_nbr FOREIGN KEY employee PRIMARY PRIMARY KEY orders PRIMARY PRIMARY KEY product category_category_code_fk FOREIGN KEY product PRIMARY PRIMARY KEY salary_grade PRIMARY PRIMARY KEY MySQL>SELECT table_name,constraint_name,constraint_type ->FROMinformation_schema.table_constraints ->WHERE table_schema=‘testy’ ->ORDER BY table_name

View the columns associated with the constraint names in the INFORMATION_SCHEMA.KEY = COLUMN_USAGE table table_name constraint_name column_name category PRIMARY category_code department PRIMARY dept_nbr employee PRIMARY employee_nbr employee employee_fk_dept_nbrdept_nbr employee emp_fk_manager manager orders PRIMARY order_nbr product PRIMARY product_nbr product category_category_code_fkcategory_code salary_grade PRIMARY grade table_name constraint_name column_name category PRIMARY category_code department PRIMARY dept_nbr employee PRIMARY employee_nbr employee employee_fk_dept_nbrdept_nbr employee emp_fk_manager manager orders PRIMARY order_nbr product PRIMARY product_nbr product category_category_code_fkcategory_code salary_grade PRIMARY grade MySQL>SELECT table_name,constraint_name, column_name ->FROMinformation_schema.key_column_usage ->WHERE table_schema=‘testy’ ->ORDER BY table_name

To view the constraints associated with an individual table use the SHOW CREATE TABLE statement. MySQL> show create table employee; CREATE TABLE “employee” ( “employee_nbr” int(4) NOT NULL, “name” varchar(10) character set latin1 collate latin1_bin default NULL, “job” varchar(10) character set latin1 collate latin1_bin default NULL, “manager” int(4) default NULL, “hire_date” datetime default NULL, “salary” decimal(7,2) default NULL, “commission” decimal (7,2) default NULL, “dept_nbr” int(2) NOT NULL, PRIMARY KEY (“employee_nbr”), KEY “employee_fk_dept_nbr” (“dept_nbr”), KEY “emp_fk_manager” (“manager”), CONSTRAINT “employee_fk_dept_nbr” FOREIGN KEY (“dept_nbr) REFERENCES “department” (“dept_nbr”) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT “emp_fk_manager” FOREIGN KEY (“manager”) REFERENCES “employee” (“employee_nbr”) ON DELETE NO ACTION ON UPDATE NO ACTION ) 1 row in set (0.00 sec)

Create the following types of constraints: NOT NULL PRIMARY KEY Query the USER_CONSTRAINTS table to view all constraint definitions and names.