开发者

How do I create this relational table?

开发者 https://www.devze.com 2023-02-17 12:49 出处:网络
Below is a portion of relational data base. I know how to create table Film and assign primary key to it.. but don\'t understate creating other tables and assigning primary key to it.

Below is a portion of relational data base. I know how to create table Film and assign primary key to it.. but don't understate creating other tables and assigning primary key to it. Any help?

How do I create this relational table?

table Film

CREATE TABLE Film (
  Id INTEGER PRIMARY KEY,
  Title VARCHAR(35) NOT NULL,
  Description VARCHAR(256) NOT NULL,
  Year INTEGER NOT NULL CHECK (Year > 1900),
  Rating INTEGER NOT NULL DEFAULT 3 CHECK (Rating BETWEEN 1 AND 5)
);

how do I create tabl开发者_运维百科e FilmFormat and OrderItem ?


CREATE TABLE `jy` (
  `PKfield` INTEGER UNSIGNED NOT NULL DEFAULT NULL AUTO_INCREMENT,
  `field2` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`PKfield`),
  CONSTRAINT `FK` FOREIGN KEY `FK` (`PKfield`)
    REFERENCES `Film` (`Id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT
)
ENGINE = InnoDB;


This is enough to show you how to create a foreign key constraint between the two tables. As commented, the CHECK constraint will be parsed but ignored.

CREATE TABLE Film (
  Id INTEGER PRIMARY KEY,
  Title VARCHAR(35) NOT NULL,
  Description VARCHAR(256) NOT NULL,
  Year INTEGER NOT NULL CHECK (Year > 1900),
  Rating INTEGER NOT NULL DEFAULT 3 CHECK (Rating BETWEEN 1 AND 5)
);

CREATE TABLE FilmFormat (
  FilmId INTEGER not null,
  FormatId INTEGER not null,
  Price decimal(16,4) null,
  Primary Key(FilmId, FormatId),
  Constraint FK_FilmFormat_FilmId FOREIGN KEY (FilmId) REFERENCES Film(Id)
);

Doing the last table will just be doing your work for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消