Download presentation
Presentation is loading. Please wait.
Published byWillis Hoover Modified over 8 years ago
2
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints
3
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
4
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.
5
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));
6
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,...),
7
Ensures that null values are not permitted for the column Employee employee_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 contain a null value for this column) Absence of NOT NULL constraint (any row can contain null for this column) NOT NULL constraint
8
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);
9
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)
10
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));
11
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);
12
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.
13
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
14
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
15
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)
16
Create the following types of constraints: NOT NULL PRIMARY KEY Query the USER_CONSTRAINTS table to view all constraint definitions and names.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.