Constraints database field

Constraints in the database I summed up as far as I know there are several of the enumerated constraints in the field of database yo a very content and he can reverse a rule input

In order to prevent future forget so now I enumerated a reference to you and me

1) primary key constraint (primary key)

Primary key constraint is usually used in the id field he has two features can not be empty or can not be repeated,

Primary key constraint (primary key)

  For example: create table table name (

          uid number(20) primary key ,

          uname varchar2(6) 

          );

2) non-empty constraint (not null)

  Non-null constraint is to ensure that a field can not be blank, a null value is not present, if the inserted null, an error is reported.

3) The only constraint (unique)

  If you want a unique field, it was the only plus unique constraint when inserting the same values ​​will complain.

  For example: create table table name (

            uname varchar2(6) unique,

            uid number(10) primary keye

           );

4) Check the constraint (check (field name in (a legal range)))

   Use check constraint can be used to constrain the range of valid values ​​for a field. For example represent male, 2 female with 1.

    create table 表名(    

            gender number(1) check (gender in(1,2)),

           );

5) foreign key constraint

   For foreign key constraints between the two tables, used to ensure the integrity of the associated data. Orders and Order Details tables for example, many relationship.

  Creating the Orders table:

    create table 表名(

             order_id number(10),

             total_price number(10,2),

             );

   Create an order list:

      create table order_detail(

                 detail_id number(10),

                 order_id number (10), // order in the table is the primary key

                 item_name varchar2(10),

                 quantity number(10),

                 constraint order_detail_detail_id_pk primary key (detail_id),

                 constraint order_detail_order_id_fk foreign key (order_id) referencs orders(order_id)|on delete cascade|on update cascade

              );

6) increment (identity)

    When used in conjunction identity appears he can go do not you go automatically increase human input.

    identity (1,1) increment space from the beginning of each increment of 1 1

Reference from: https: //www.cnblogs.com/sunxuchu/p/5578057.html

Reference from: https: //www.cnblogs.com/jasonboren/p/10921508.html

 

Guess you like

Origin www.cnblogs.com/zsznh/p/11139808.html