MySQL Constraint-Instance

Source: Internet
Author: User

"Example 2" creates a customer reservation form in the hotel management system and sets the customer number and room number to a federated primary key, the table structure is as follows:

Number

Data type

Customer ID

The ID of the

Check in Date

Check out Date

Amount

7,2 )

Remarks

$ )

The CREATE TABLE ordeinfo

(

CUSTOMER ID INTEGER,

The ID INTEGER,

CHECK in DATE DATETIME

CHECK out DATE DATETIME,

AMOUNT NUMERIC (7,2),

REMARKS Varcher (200)

Pimary KEY (Customerid,orderid)

);”

3 Adding a PRIMARY KEY constraint when modifying a table

To add a primary key constraint to a single column in the table if the data table already exists, the syntax is:

"ALTER TABLE table_name

ADD CONSTRAINT pk_name PRIMARY KEY (column name) "

"Example 1" creates a room information table (Roominfo) in the hotel management system, with the following table structure:

Number

Data type

Roominfo

The typeID

Roomprice

7,2 )

Roomstate

2 )

Roomark

$ )

The code for creating the Room Information table (Roominfo) is as follows:

Crreate TABLE Roominfo

(

Roominfo INTEGER,

Roomtypeid INTEGER,

Roomprice NUMERIC (7,2),

Roomstate VARCHAR (2),

Roomark VARCHAR (200)

);

To add a primary key constraint to the Roomid column in the Room Information table (roominfo), the code is as follows

ALTER TABLE Roominfo

(

ADD CONSTRAINT pk_roominfo PRIMARY KEY (roomid)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.