18 SQL language - Constraints

Understanding Mandatory 

Jump to resolve constraints w3school

problem found

Now we create a table Student

create table student(
       snume number(10),
       sname varchar2(100),
       sex char(4),
       age number,
       qq number,
       sal number(6,2),
       mail varchar2(50)
);

  

Once created, faced with the following questions:

Question 1: school repeated, data can also be successfully inserted into the
insert into student (snum, sna, sex, age, qq, sal, mail) values ( '123', ' John Doe', 'M', 18,12345678, 88.88, '12345678 @ qq.com')
INSERT iNTO Student values (123, 'John Doe', 'M', 18,7890122,88.99, '7890122 @ qq.com ')
question 2: name can be empty.
insert into student values (456, 'Liu Yan', 'female', 18,666999,99.66, '666 999 @ qq.com')
INSERT INTO Student (snum, Sex, Age, QQ, SAL, mail) values (789, ' female ', 18,888444,99.66,' 888444 @ qq.com ')
question 3: sex is not only empty, but also that others do not know the character
insert into student (snum, sna, age, qq, sal, mail) values (108, 'Jing Tian', 18,000999,99.66, '000999 @ qq.com')
INSERT INTO Student (SNUM, SNA, Sex, Age, QQ, SAL, mail) values (102, 'Jing Tian' 18,000999,99.66, '000999 @ qq.com' ' a',

Question 4: Age can be more than 200
INSERT INTO Student (snum, the SNA, Sex, Age, QQ, SAL, mail) values (103, 'Tang Yan', 'female', 23,78900,99.66, '78900 @ qq.com ' )
INSERT INTO Student (SNUM, SNA, Sex, Age, QQ, SAL, mail) values (103, 'Tang Yan', 'F', 230,78900,99.66, '78900 @ qq.com ')
issue 5: qq No. consistent
insert into student (snum, sna, sex, age, qq, sal, mail) values (104, ' off Xiaotong', 'F', 19,111000,99.66, '111000 @ qq.com')
INSERT INTO Student (SNUM , sna, sex, age, qq , sal, mail) values (105, ' Yuan Hua', 'M', 22,111000,99.66, '111000 @ qq.com ')

Solve the problem

Three ways

1. When creating a table, written on the back field

create table student(
   sname varchar2(10) not null primary key ,
    

2. Create a table, written in the last unified face

create table student1(
       snum number(10) not null primary key,
       sname varchar2(100),
       sex char(4),
       age number(10),
       qq number(30),
       sal number(6,2),
       mail varchar2(50)
      constraints pk_ table _ column name primary key (field names),
      constraints ck_ table _ field name check (field name is not null),
      constraints uk_ table _ column names unique (field names)
);

  Wherein the check (condition) can be written as: check (age> 0 and age <100) get it

3. The table has been created, go to Add / Edit / Delete

alter table table _ add constraints pk_ field name table primary key (field name);

alter table drop constraints pk_ table name field name table _

 

 

Question 1: school repeated, data can also be inserted successfully

Using the primary key constraint: Student ID is a unique identification data, it must be unique and can not be empty
--- (1), in determining the primary key field to add primary key keyword
--- (2), the table creation later with: constraints pk_ _ field name table primary key (field)
--- (3), using alter table table _ add constraints pk_ field name table primary key (field) after creating the table;
- - 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), after creating the table using the alter table modify table field name type Not null;
--- (. 4), to modify field may store a null value: alter table modify table field name type null;

Question 3: Sex is not only empty, but also that the other characters do not know

Use check constraints
--- (1), create a table when using the default value check (condition) in the field, but will be allowed null values, and the default value only take effect in the case of field is not declared
--- (2 ), after you create the table all the fields to use: constraints ck_ table _ column names check (condition)
--- (3), after creating a table using: alter table table name add constraints ck_ table _ field name check ( condition)

Question 4: Age can be more than 200

- Use check constraint

Question 5: No. consistent qq

Use only constraint
- (1), directly using the unique key fields
- (2), used in all the fields: constraints uk_ table _ unique field names (column names)
- (. 3), ALTER Table table add constraints uk_ table _ column names unique (field names)
- drop the unique constraints: alter table table name drop constraints uk_ table name field name _

 

 

Guess you like

Origin www.cnblogs.com/Scorpicat/p/12304437.html