PRIMARY KEY
Primary Key (Inline)
CREATE TABLE IF NOT EXISTS `course`(
`course_id` CHAR(12) NOT NULL PRIMARY KEY,
`course_code` VARCHAR(40) NOT NULL,
`course_title` VARCHAR(40) NOT NULL,
`course_dept` VARCHAR(4) NOT NULL,
`course_credit` FLOAT(3,1),
CHECK (`course_credit` IN (3.0, 1.5))
);
SHOW INDEX FROM `course`;
Primary Key
CREATE TABLE IF NOT EXISTS `course`(
`course_id` CHAR(12) NOT NULL,
`course_code` VARCHAR(40) NOT NULL,
`course_title` VARCHAR(40) NOT NULL,
`course_dept` VARCHAR(4) NOT NULL,
`course_credit` FLOAT(3,1),
CHECK (`course_credit` IN (3.0, 1.5)),
PRIMARY KEY (`course_id`)
);
SHOW INDEX FROM `course`;
Composite Primary Key
CREATE TABLE IF NOT EXISTS `course`(
`course_id` CHAR(12) NOT NULL,
`course_code` VARCHAR(40) NOT NULL,
`course_title` VARCHAR(40) NOT NULL,
`course_dept` VARCHAR(4) NOT NULL,
`course_credit` FLOAT(3,1),
CHECK (`course_credit` IN (3.0, 1.5)),
PRIMARY KEY (`course_id`, `course_code`)
);
SHOW INDEX FROM `course`;
Alter Table
CREATE TABLE IF NOT EXISTS `course`(
`course_id` CHAR(12) NOT NULL,
`course_code` VARCHAR(40) NOT NULL,
`course_title` VARCHAR(40) NOT NULL,
`course_dept` VARCHAR(4) NOT NULL,
`course_credit` FLOAT(3,1),
CHECK (`course_credit` IN (3.0, 1.5))
);
ALTER TABLE `course` ADD CONSTRAINT `PK_course` PRIMARY KEY (`course_id`);
SHOW INDEX FROM `course`;
Drop Primary Key
CREATE TABLE IF NOT EXISTS `course`(
`course_id` CHAR(12) NOT NULL,
`course_code` VARCHAR(40) NOT NULL,
`course_title` VARCHAR(40) NOT NULL,
`course_dept` VARCHAR(4) NOT NULL,
`course_credit` FLOAT(3,1),
CHECK (`course_credit` IN (3.0, 1.5))
);
ALTER TABLE `course` ADD CONSTRAINT `PK_course` PRIMARY KEY (`course_id`);
SHOW INDEX FROM `course`;
ALTER TABLE `course` DROP PRIMARY KEY;
SHOW INDEX FROM `course`;
Last updated
Was this helpful?