当前位置:网站首页>Database - integrity constraints

Database - integrity constraints

2022-06-26 15:10:00 EbowTang

In database design , The data in the table has a certain range of values and connections , The data between multiple tables sometimes have certain reference relationships . When creating and modifying tables , Data integrity and consistency can be ensured by defining constraints . Constraints are rules , Inserting data 、 These rules should be verified when deleting and modifying , So as to play a restrictive role .

The naming rules recommend : Constraint type _ Constraint fields :

  • Non empty constraint :NN Table name Name
  • Unique constraint :UK Table name Name
  • Primary key constraint :PK_ Table name
  • Foreign key constraints :FK Table name Name
  • Check constraint :CK Table name Name

About table level constraints and column level constraints :

  • Column level constraints can only act on one column
  • Table level constraints can act on multiple columns ( Of course, table level constraints can also act on a column )
  • Define the way : The column constraint must follow the definition of the column , Table constraints do not work with columns , It is defined separately .
  • Non empty (not null) Constraints can only be defined on columns

stay KingbaseES Tested on the database :

1, The creation of a table , And its constraint definitions :

        1) Define the constraint method in the direct table

        2) Define the table before defining the constraint

2, Test the primary key , Non empty , Check , Only four constraints

 
drop table if exists student; --oracle Without this grammar ,mysql and KES There is 
 
----------------------------------------------
----- piecewise 1: Create and test constraint tables 
----------------------------------------------
---1, Directly define constraints on tables and columns 
CREATE TABLE student (
	stu_nmb number(8) primary key,  -- Primary key 
	stu_name char(8) not null,  -- Non empty constraint 
	gender varchar2(2) check (gender in (' male ',' Woman ')),  -- Check constraint 
	age number(2) CHECK (age BETWEEN 18 AND 30),   -- Check constraint 
	class varchar2 (40) not null,
	email varchar2 (30) UNIQUE,
	sdate DATE
);
 
---2, Another form of defining constraints : Define columns first, then define constraints 
-- doubt , Non empty constraints do not support the following ?
--constraint  con_name not null (stu_name),
CREATE TABLE student (
	stu_nmb number(8),
	stu_name char(8) not null,
	gender varchar2(2),
	age number(2),   -- Check constraint 
	class varchar2 (40) not null,
	email varchar2 (30),
	sdate DATE,
	constraint  con_nmb primary key (stu_nmb),
	constraint con_check check ( gender in (' male ',' Woman ')),
	constraint ch_age	CHECK (age BETWEEN 18 AND 30),   -- Check constraint 
	constraint uk_student_email UNIQUE (email)
);
 
select * from student;
 
--- Constraint testing 
-- First insert , correct 
INSERT INTO student VALUES(2314,' Zhang Detian ',' male ',19,' Senior 3 6 class ','[email protected]',SYSDATE);
-- Duplicate primary key , Report errors 
INSERT INTO student VALUES(2314,' Wu Haifeng ',' male ',18,' Senior 3 1 class ','[email protected]',SYSDATE);
-- In violation of the gender constraint , Report errors 
INSERT INTO student VALUES(2614,' Wudetian ',' change ',29,' Senior 3 8 class ','[email protected]',SYSDATE);
-- Violation of non empty constraints , Report errors 
INSERT INTO student VALUES(2614,'',' Woman ',24,' Senior 3 3 class ','[email protected]',SYSDATE);
-- Only constraint check , Report errors 
INSERT INTO student VALUES(9874,' Zhanghuale ',' Woman ',19,' Senior 3 4 class ','[email protected]',SYSDATE);
 
 
----------------------------------------------
----- piecewise 2: Define constraints after creating tables 
----------------------------------------------
--3, Unconstrained tables , Now create it 
CREATE TABLE student (
	stu_nmb number (8),
	stu_name char (8),
	gender varchar2 (2),
	age number (2),
	class varchar2 (40),
	email varchar2 (30),
	sdate DATE
);
 
-- Start creating constraints 
alter table student add constraint pk_nmb primary key (stu_nmb); --oracle Need to be in add Put parentheses after 
alter table student add constraint ck_gender check (gender in (' male ',' Woman '));--oracle Need to be in add Put parentheses after 
-- Non null constraints and oracle Difference is very big 
--oracle:alter table student modify(stu_name constraint con_name not null);
ALTER TABLE student ALTER COLUMN stu_name SET NOT NULL; 
ALTER TABLE student ADD CONSTRAINT uq_email UNIQUE (email);
 
select * from student;
 
--- Constraint testing 
-- First insert , correct 
INSERT INTO student VALUES(2314,' Zhang Detian ',' male ',19,' Senior 3 6 class ','[email protected]',SYSDATE);
-- Duplicate primary key , Report errors 
INSERT INTO student VALUES(2314,' Wu Haifeng ',' male ',18,' Senior 3 1 class ','[email protected]',SYSDATE);
-- Violation of inspection constraints , Report errors 
INSERT INTO student VALUES(2614,' Wudetian ',' change ',29,' Senior 3 8 class ','[email protected]',SYSDATE);
-- Violation of non empty constraints , Report errors 
INSERT INTO student VALUES(2614,'',' Woman ',24,' Senior 3 3 class ','[email protected]',SYSDATE);
-- Only constraint check , Report errors 
INSERT INTO student VALUES(9874,' Zhanghuale ',' Woman ',19,' Senior 3 4 class ','[email protected]',SYSDATE);
 
-- Remove the four types of constraints for the specified column 
ALTER TABLE student ALTER COLUMN stu_name DROP NOT NULL;
ALTER TABLE student DROP CONSTRAINT uq_email;
ALTER TABLE student DROP CONSTRAINT ck_gender;
ALTER TABLE student DROP CONSTRAINT pk_nmb;

-- Disable and enable constraints 
ALTER TABLE student DISABLE CONSTRAINT con_nmb
ALTER TABLE student ENABLE CONSTRAINT con_nmb
INSERT INTO student VALUES(2314,' Wangdetian ',' male ',21,' Senior 3 1 class ','[email protected]',SYSDATE);

原网站

版权声明
本文为[EbowTang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261452029207.html

随机推荐