Database---primary key constraint

1. Set the primary key constraint
(1) Method 1: When creating a table, declare the specified field as the primary key in the field description;
CREATE TABLE table name (
field type (length) PRIMARY KEY,
field type (length)
);

CREATE TABLE STUDENT(
STU_ID INT PAIMARY KEY ,
STU_NAME VARCHAR(255)
);

(2), Method 2: When creating a table, declare the specified field as the primary key in the CONSTRAINT constraint area
. Format: [CONSTRAINT name] PRIMARY KEY (field list).
· Where CONSTRAINT can be omitted, if you want to name the primary key, CONSTRAING cannot be omitted.
The set primary key needs to be enclosed in parentheses. If there are multiple fields that need to be used, they can be separated. Declare two fields as the primary key, which we call the joint primary key
CREATE TABLE table name (
field name type (length),
field name type ( length),
[CONSTRAING name] PRIMARY KEY (field name, field name)
);
CREATE TABLE STUDENT02(
STU_ID INT,
STU_NAME VARCHAR(255),
[CONSTRAINT STUDENT_KEY] PRIMARY KEY (STU_ID)
);

(3) Method 3: After creating the table, declare the specified field as the primary key by modifying the "table structure".
CREATE TABLE table name (
field type (length),
field type (length)
);
ALTER TABLE table name ADD PRIMARY KEY (field name);

CREATE TABLE STUDENT(
STU_ID INT,
STU_NAME VARCHAR(255)
);
ALTER TABLE STUDENT ADD PRIMARY KEY (STU_ID);

2. Delete the primary key
ALTER TABLE 表名 DROP PRIMARY KEY;
ALTER TABLE STUDENT03 DROP PRIMARY KEY;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955039&siteId=291194637