16
Including Constraints

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

Including Constraints

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

Objectives

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

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

What Are 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 NULLPRIMARY KEYREFERENCESCheck

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

Constraint Guidelines

Constraints are named.Create a constraint:

At the same time as the table is createdAfter the table has been created

Define a constraint at the column or table level.View a constraint in the data dictionary.

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

Defining ConstraintsCREATE 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));

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

Defining Constraints

Column constraint level

Table constraint level

column [CONSTRAINT constraint_name] constraint_type,

column,...[CONSTRAINT constraint_name] constraint_type (column, ...),

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

The NOT NULL ConstraintEnsures that null values are not permitted for the column

Employeeemployee_nbr name job ... commission dept_nbr

7839 King President 10 7698 Blake Manager 30 7782 Clark Manager 10 7566 Jones Manager20...

NOT NULL constraint(no row may containa null value forthis column)

Absence of NOT NULL constraint(any row can containnull for this column)

NOT NULLconstraint

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

The NOT NULL Constraint

Defined at the column level

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

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

The PRIMARY KEY Constraint

Department dept_nbrdept_name location

10 Accounting New York 20 Research Dallas 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)

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

The PRIMARY KEY Constraint

Defined at either the table level or the column level

MySQL>CREATE TABLE dept( 2 dept_nbr NUMBER(2), 3 dept_name VARCHAR2(14), 4 location VARCHAR2(13), 5 PRIMARY KEY(dept_nbr));

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

Adding a Constraint

Add or drop, but not modify, a constraintEnable or disable constraintsAdd a NOT NULL constraint by using the MODIFY clause

ALTER TABLE table ADD [CONSTRAINT constraint] type (column);

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

Adding a Constraint

Add a PRIMARY KEY constraint to the EMP table indicating employee number is the primary key

MySQL>ALTER TABLE employee 2 ADD CONSTRAINT PRIMARY KEY(employee_nbr);Table altered.

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

Viewing ConstraintsQuery the INFORMATION_SCHEMA.TABLE_CONSTRAINTS table view all constraint definitions and names.

table_nameconstraint_nameconstraint_type---------- --------------- ---------------category PRIMARY PRIMARY KEYdepartment PRIMARY PRIMARY KEYemployee emp_fk_manager FOREIGN KEYemployee employee_fk_dept_nbr FOREIGN KEYemployee PRIMARY PRIMARY KEYorders PRIMARY PRIMARY KEYproduct category_category_code_fk FOREIGN KEYproduct PRIMARY PRIMARY KEYsalary_grade PRIMARY PRIMARY KEY

MySQL>SELECT table_name,constraint_name,constraint_type ->FROM information_schema.table_constraints ->WHERE table_schema=‘testy’ ->ORDER BY table_name

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

Viewing the Columns Associated with Constraints

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_codedepartment PRIMARY dept_nbremployee PRIMARY employee_nbremployee employee_fk_dept_nbrdept_nbremployee emp_fk_manager managerorders PRIMARY order_nbrproduct PRIMARY product_nbrproduct category_category_code_fkcategory_codesalary_grade PRIMARY grade

MySQL>SELECT table_name,constraint_name, column_name ->FROM information_schema.key_column_usage ->WHERE table_schema=‘testy’ ->ORDER BY table_name

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

View Constraints associated with a table

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)

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

Summary

Create the following types of constraints:NOT NULLPRIMARY KEY

Query the USER_CONSTRAINTS table to view all constraint definitions and names.