oracle-- constraint (primary key, non-empty, check)

Question 1: school repeated, data may also be successfully inserted 
						using the primary key constraint: student number is a unique identification data, it must be unique and can not be empty 
							   --- (1), is added after the primary key in the primary key field determined keyword 
							   --- (2), create a table in the back of use: constraints pk_ table _ column name primary key (field names) 
							   - (3), after creating a table using alter table table table name add constraints pk_ name _ name field primary key (field names); 
							   - Remove the primary key: alter table table name drop constraints pk_ table name field name _ 
					question 2: name can be empty. 
						A non-NULL constraints 
							   --- (1), table creation time to add not null in the back field 
							   --- (2), using the constraints ck_ table name in the field _ creating a table field names check (field name is not null) Learn 
							   - (3), using the alter table table name modify field name type not null after creating the table; 
							   --- (4), modify the field can store null values: alter table table name modify field name type null; 
					question 3: sex is not only empty, but also that the other characters do not know 
						to use check constraints
							  --- (1), create a table in the field when using default values check (condition),
							  --------- appear but will allow null values, and the default value only take effect in the case of field is not declared 
							  --- (2), using all the fields after you create the table: constraints ck_ table name _ field name check (condition) 
							  --- (3), after creating a table using: alter table table name add constraints ck_ table _ column names check (condition) 
					question 4: Age can be more than 200 
							 - use check constraint 
					problem 5: qq number is consistent 
							with a unique constraint 
							 - (1), the use of unique keywords directly in the field 
							 - (2) is used in all the fields: constraints uk_ table _ unique field names (field names) 
							 - ( 3), alter table table name add constraints uk_ table _ column names unique (field names) 
							 - drop the unique constraints: alter table table name drop constraints uk_ table name field name _

  

1, the primary key constraint

Three ways primary key constraint manner

create table student(
       sno number(10) primary key,
       sname varchar2(100),
       sage number(3),
       ssex char(4),
       sbirth date,
       sqq varchar2(30)
);
create table student(
       sno number(10),
       sname varchar2(100),
       sage number(3),
       ssex char(4),
       sbirth date,
       sqq varchar2(30),
       constraint pk_student_sno primary key(sno) ---添加主键约束
);
create table student(
       sno number(10),
       sname varchar2(100),
       sage number(3),
       ssex char(4),
       sbirth date,
       sqq varchar2(30)
);
alter table student add constraint pk_student_sno primary key(sno);
alter table student drop constraint pk_student_sno;

 

select * from student for update;
drop table student;

 

Non-empty constraint

Three ways

create table student(
       sno number(10) primary key,
       sname varchar2(100) not null,
       sage number(3),
       ssex char(4),
       sbirth date,
       sqq varchar2(30)
);

 

create table student(
       sno number(10) primary key,
       sname varchar2(100),
       sage number(3),
       ssex char(4),
       sbirth date,
       sqq varchar2(30),
       constraints ck_student_sname check(sname is not null)
);

 

create table student(
       sno number(10) primary key,
       sname varchar2(100),
       sage number(3),
       ssex char(4),
       sbirth date,
       sqq varchar2(30)
       
);

  alter table student add constraint ch_student_sname check(sname is not null);  

 

alter table student drop constraint ch_student_sname 

 

Check constraints

create table student(
       sno number(10) primary key,
       sname varchar2(100) not null,
       sage number(3) check(sage<150 and sage>0),
       ssex char(4) check(ssex ='男' or ssex = '女'),
       sbirth date,
       sqq varchar2(30)      
);

 

create table student(
       sno number(10) primary key,
       sname varchar2(100) not null,
       sage number(3),
       ssex char(4) check(ssex ='男' or ssex = '女'),
       sbirth date,
       sqq varchar2(30),
       constraint ch_student_sage check(sage<150 and sage>0)      
);

  

create table student(
       sno number(10) primary key,
       sname varchar2(100) not null,
       sage number(3),
       ssex char(4) check(ssex ='男' or ssex = '女'),
       sbirth date,
       sqq varchar2(30)     
);

alter table student add constraint ch_student_sage check(sage<150 and sage>0);

 

alter table student drop constraint ch_student_sage;

  

 

 

Guess you like

Origin www.cnblogs.com/eadela/p/11479832.html