How do I enforce uniqueness in a table?

后端 未结 4 872
慢半拍i
慢半拍i 2020-12-21 11:03

For example, I have to program an enrolment table which has:

  1. enrolment_id
  2. academic_period
  3. results
  4. student_id
  5. subject
  6. <
4条回答
  •  一向
    一向 (楼主)
    2020-12-21 11:42

    Add a unique constraint. From here:

    To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax:

    MySQL / SQL Server / Oracle / MS Access:

    CREATE TABLE Persons
     (
     P_Id int NOT NULL,
     LastName varchar(255) NOT NULL,
     FirstName varchar(255),
     Address varchar(255),
     City varchar(255),
     CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
     )
    

    (Modify the above for the column(s) you need to set as unique)

提交回复
热议问题