sql 高級 (四)(SQL 約束 (Constraints))

SQL 約束

  • NOT NULLsql

  • UNIQUE函數

  • PRIMARY KEYspa

  • FOREIGN KEYci

  • CHECKit

  • DEFAULTtable

        UNIQUE 和 PRIMARY KEY 約束均爲列或列集合提供了惟一性的保證。ast

        區別:每一個表能夠有多個 UNIQUE 約束,可是每一個表只能有一個 PRIMARY KEY 約束。date

 unique  im

sql  unique   Constraint   on   create   table(表未被建立
數據

MySql  

            CREATE TABLE Persons

            (

                Id_P int NOT NULL,

                LastName varchar(255) NOT NULL,

                FirstName varchar(255),

                Address varchar(255),

                City varchar(255),

                UNIQUE (Id_P)

            )

SQL Server / Oracle / MS Access:

            CREATE TABLE Persons

            (

                Id_P int NOT NULL UNIQUE,

                LastName varchar(255) NOT NULL,

                FirstName varchar(255),

                Address varchar(255),

                City varchar(255)

            )

MySQL / SQL Server / Oracle / MS Access(多個列定義 UNIQUE 約束):

            CREATE TABLE Persons

            (

                Id_P int NOT NULL,

                LastName varchar(255) NOT NULL,

                FirstName varchar(255),

                Address varchar(255),

                City varchar(255),

                CONSTRAINT   uc_PersonID   UNIQUE (Id_P,LastName)

            )

表已被建立

             alter  table  Persons   add   unique(Id_P)

             alter  table  Persons   add   constraint   uc_PersonID   UNIQUE (Id_P,LastName) 

撤銷 UNIQUE 約束

            MySql

                    alter   table  Persons   drop   index  uc_PersonID

          SQL Server / Oracle / MS Access:

                    alter    table  Persons   drop   constraint     uc_PersonID         

PRIMARY KEY            

        MySQL:

                CREATE TABLE Persons

                    (

                        Id_P int NOT NULL,

                        LastName varchar(255) NOT NULL,

                        FirstName varchar(255),

                        Address varchar(255),

                        City varchar(255),

                        PRIMARY KEY (Id_P)

                    )

        SQL Server / Oracle / MS Access:

                CREATE TABLE Persons

                (

                Id_P int NOT NULL PRIMARY KEY,

                LastName varchar(255) NOT NULL,

                FirstName varchar(255),

                Address varchar(255),

                City varchar(255)

                )

        MySQL / SQL Server / Oracle / MS Access(爲多個列定義 PRIMARY KEY 約束):

                CREATE TABLE Persons

                (

                  Id_P int NOT NULL,

                  LastName varchar(255) NOT NULL,

                  FirstName varchar(255),

                  Address varchar(255),

                 City varchar(255),

                 CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)

                )

表已被建立

    MySQL / SQL Server / Oracle / MS Access:

            ALTER   TABLE    Persons    ADD    PRIMARY   KEY (Id_P)

爲多個列定義 PRIMARY KEY 約束 

        MySQL / SQL Server / Oracle / MS Access:

            ALTER    TABLE    Persons    ADD CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)


撤銷 PRIMARY KEY 約束

            MySQL:

            ALTER     TABLE     Persons     DROP  PRIMARY     KEY

            SQL Server / Oracle / MS Access:

            ALTER     TABLE     Persons      DROP     CONSTRAINT     pk_PersonID



FOREIGN KEY 約束   

        FOREIGN KEY 約束用於預防破壞表之間鏈接的動做。

        FOREIGN KEY 約束也能防止非法數據插入外鍵列,由於它必須是它指向的那個表中的值之一。

SQL  FOREIGN  KEY  Constraint  on  CREATE  TABLE

        MySQL:

            CREATE TABLE Orders

            (

                Id_O int NOT NULL,

                OrderNo int NOT NULL,

                Id_P int,

                primary   key (Id_O),

                foreign  key (Id_P) references Persons(Id_P)

            )

        SQL Server / Oracle / MS Access:

        CREATE TABLE Orders

        (

            Id_O int NOT NULL primary key,

            OrderNo int NOT NULL,

            Id_P int  foreign   key   references Persons(Id_P)

        )

爲多個列定義 FOREIGN KEY 約束:

MySQL / SQL Server / Oracle / MS Access:

        CREATE TABLE Orders

        (

            Id_O int NOT NULL,

            OrderNo int NOT NULL,

            Id_P int,

            PRIMARY KEY (Id_O),

            constraint    fk_PerOrders   foreign key (Id_P)

            references    Persons(Id_P)

        )

表已存在

MySQL / SQL Server / Oracle / MS Access:

        ALTER TABLE Orders

        ADD foreign KEY (Id_P)

        references Persons(Id_P)


爲多個列定義 FOREIGN KEY 約束

      MySQL / SQL Server / Oracle / MS Access:

        ALTER   TABLE   Orders

        Add    constraint     fk_PerOrders

        Foreign  Key (Id_P)

        References   Persons(Id_P)

撤銷 FOREIGN KEY 約束

        MySQL:

        ALTER  TABLE  Orders

        DROP   foreign   key  fk_PerOrders


        SQL Server / Oracle / MS Access:

        ALTER  TABLE  Orders

        DROP  constraint    fk_PerOrders


CHECK 約束

                (CHECK 約束用於限制列中的值的範圍。)

        若是對單個列定義 CHECK 約束,那麼該列只容許特定的值。

        若是對一個表定義 CHECK 約束,那麼此約束會在特定的列中對值進行限制。

SQL CHECK Constraint on CREATE TABLE

My SQL:

    CREATE TABLE Persons

    (

    Id_P int NOT NULL,

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),

    City varchar(255),

    check (Id_P>0)

    )

SQL Server / Oracle / MS Access:

    CREATE TABLE Persons

    (

    Id_P int NOT NULL  check (Id_P>0),

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),

    City varchar(255)

    )

爲多個列定義 CHECK 約束:

   MySQL / SQL Server / Oracle / MS Access:

        create  table   Persons

    (

        Id_P int NOT NULL,

        LastName varchar(255) NOT NULL,

        FirstName varchar(255),

        Address varchar(255),

        City varchar(255),

        constraint   chk_Person check (Id_P>0  and  City='Sandnes')

    )

表已存在

MySQL / SQL Server / Oracle / MS Access:

    alter  table  Persons

    Add  check  (Id_P>0)

爲多個列定義 CHECK 約束

MySQL / SQL Server / Oracle / MS Access:

    alter   table  Persons

    ADD   constraint   chk_Person CHECK (Id_P>0 AND City='Sandnes')

撤銷 CHECK 約束

SQL Server / Oracle / MS Access:

        alter   table   Persons

        drop   constraint    chk_Person

MySQL:

        alter   table   Persons

        drop   check   chk_Person

SQL DEFAULT 約束

        DEFAULT 約束用於向列中插入默認值。

        若是沒有規定其餘的值,那麼會將默認值添加到全部的新記錄。

SQL DEFAULT Constraint on CREATE TABLE

My SQL / SQL Server / Oracle / MS Access:

        CREATE TABLE Persons

        (

        Id_P int NOT NULL,

        LastName varchar(255) NOT NULL,

        FirstName varchar(255),

        Address varchar(255),

        City varchar(255) DEFAULT 'Sandnes'

        )

        經過使用相似 GETDATE() 這樣的函數,DEFAULT 約束也能夠用於插入系統值

    CREATE TABLE Orders

    (

        Id_O int NOT NULL,

        OrderNo int NOT NULL,

        Id_P int,

        OrderDate date DEFAULT GETDATE()

    )

表已存在

MySQL:

        alter  table   Persons

        alter  City   set   default 'sandes'

SQL Server / Oracle / MS Access:

        alter  table Persons

        alter   column   City   set   default  'sandnes'

撤銷 DEFAULT 約束

MySQL:

        alter   table  Persons

        alter  City drop default

SQL Server / Oracle / MS Access:

        alter   table  Persons

        alter  column city  drop  default

相關文章
相關標籤/搜索