Ch3 the relational model(笔记+习题)

笔记:

An important component of a data model is the set of constructs it provides for specifying conditions that must be satisfied by the data. Such conditions, called integrity constraints (ICs), enable the DBMS to reject operations that might corrupt the data.
A relation consists of a relation schema and a relation instance.

CREATE TABLE Students (sid CHAR(20),name CHAR(30),login CHAR(20),age INTEGER,gpa REAL)

INSERT
INTO Students (sid, name, login, age, gpa)
VALUES (53688, `Smith', `smith@ee', 18, 3.2)

DELETE
FROM Students S
WHERE S.name = `Smith'

UPDATE Students S
SET S.age = S.age + 1, S.gpa = S.gpa - 1
WHERE S.sid = 53688

Review:

1、关系模型的主要元素是关系。 关系模式通过指定关系名称和每个字段的名称来描述关系的结构。 另外,关系模式包括域约束,这是对关系字段的类型限制。 字段数称为关联度。 关系实例是一个实际表,其中包含一组遵循关系模式的元组。 元组的数量称为该关系的基数。 SQL-92是用于与DBMS进行交互的标准语言。 它的数据定义语言(DDL)可以创建关系(创建表)和修改关系(删除,更新)。(第3.1节)
2、完整性约束是每个合法数据库实例都必须满足的数据库架构条件。除了域约束之外,IC的其他重要类型还包括键约束(唯一标识元组的最小字段集)和外键约束(一种关系中的字段引用另一种关系中的字段)。 SQL-92支持上述类型的IC的规范,以及更通用的约束,称为表约束和断言。 (第3.2节)
3、只要关系被修改,IC就会强制执行,并且指定的IC可能会与修改冲突。 对于外键约束违规,SQL-92提供了几种替代方案来处理违规:NO ACTION,CASCADE,SET DEFAULT和SET NULL。 (第3.3节)
4、关系数据库查询是关于数据的问题。 SQL支持非常有表现力的查询语言。 (第3.4节)
5、ER模型构造有标准的翻译成SQL。 实体集映射到关系中。 没有约束的关系集也映射到关系中。 当翻译具有约束,弱实体集,类层次结构和聚合的关系集时,映射会更加复杂。 (第3.5节)
6、视图是一个关系,其实例未显式存储,但根据需要进行计算。 除了通过视图定义外部模式来实现逻辑数据独立性之外,出于安全原因,视图在限制对数据的访问方面也起着重要作用。 由于视图可能是通过复杂的查询定义的,因此处理针对视图指定的更新非常复杂,并且SQL-92对于何时可更新视图具有非常严格的规则。 (第3.6节)
7、SQL提供了语言构造来修改表的结构(ALTER TABLE)和销毁表和视图(DROP TABLE)。 (第3.7节)

Exercise:

Exercise 3.1 Define the following terms: 

relation schema:指定关系的名称,字段(或列或属性)的名称和域。
relational database schema:数据库中关系模式的集合。
domain:域。
relation instance:是一组记录,每个记录具有与关系模式相同的字段数。
relation cardinality:行数。
relation degree:字段数。

A key constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for a tuple.

Exercise 3.2 How many distinct tuples are in a relation instance with cardinality 22?
22。

Exercise 3.3 Does the relational model, as seen by an SQL query writer, provide physical
and logical data independence? Explain.
SQL查询的用户不知道数据在物理上如何存储,只依赖于抽象关系来查询,物理数据独立性因此保证。用户可以定义视图,因此逻辑数据独立性也可以通过使用视图定义来隐藏概念模式的变化而得以保证。

Exercise 3.4 What is the difference between a candidate key and the primary key for a given relation? What is a superkey?
超键:在关系中能唯一标识元组的属性集合
候选键是不含有多余属性的超键
主键是用户选作标识元组的一个候选键

Exercise 3.5 Consider the instance of the Students relation shown in Figure 3.1.
1. Give an example of an attribute (or set of attributes) that you can deduce is not a
candidate key, based on this instance being legal.
name、age不是候选键,因为其不能唯一标识元组。
2. Is there any example of an attribute (or set of attributes) that you can deduce is a
candidate key, based on this instance being legal?
sid、login是候选键,因为其能唯一标识元组。

Exercise 3.6 What is a foreign key constraint? Why are such constraints important? What
is referential integrity?
外键是指另一张表的主键。外键的主要作用是保持数据的一致性、完整性。引用完整性是指有外键约束的父方更新,子方也需要更新。

Exercise 3.7 Consider the relations Students, Faculty, Courses, Rooms, Enrolled, Teaches,
and Meets In that were defined in Section 1.5.2.

1. List all the foreign key constraints among these relations.
Students, Faculty, Courses, Rooms不存在外键约束。在Enrolled关系中,sid和cid都有外键约束(每个学生都应该注册课程)。在Teaches关系中,fid和cid也有外键约束。在Meets关系中,cid和rno有外键约束。
2. Give an example of a (plausible) constraint involving one or more of these relations that is not a primary key or foreign key constraint.
sid、cid、fid的长度可能被限制。

Exercise 3.8 Answer each of the following questions briefly. The questions are based on the following relational schema:

Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pct time: integer)
Dept(did: integer, dname: string, budget: real, managerid: integer)
1. Give an example of a foreign key constraint that involves the Dept relation. What are
the options for enforcing this constraint when a user attempts to delete a Dept tuple?

如果把Works中的did当作外键,引用Dept:
CREATE TABLE Works( eid        INTEGER NOT NULL ,
                                        did        INTEGER NOT NULL ,
                                        pcttime INTEGER,
                                        PRIMARY KEY (eid,did),
                                        UNIQUE (eid),
                                        FOREIGN KEY (did) REFERENCES Dept)
当用户尝试删除一个Dept元组时,有4种选择:
·删除引用了此Dept的所有Works。
·如果有Work引用了此Dept,不允许删除。
·对于引用了此Dept的所有Works,其did字段设置为默认。
·对于引用了此Dept的所有Works,其did字段设置为null。

2. Write the SQL statements required to create the above relations, including appropriate
versions of all primary and foreign key integrity constraints.

CREATE TABLE Emp( eid        INTEGER,
                                     ename  CHAR(10),
                                     age       INTEGER,
                                     salary    REAL,
                                     PRIMARY KEY (eid))
CREATE TABLE Works( eid        INTEGER,
                                        did        INTEGER,
                                        pcttime INTEGER,
                                        PRIMARY KEY (eid,did),
                                        FOREIGN KEY (eid) REFERENCES Emp,
                                        FOREIGN KEY (did) REFERENCES Dept,
                                        ON DELETE CASCADE)
CREATE TABLE Dept( did            INTEGER,
                                      budget      REAL,
                                      managerid INTEGER,
                                      PRIMARY KEY (did),
                                      FOREIGN KEY (managerid) REFERENCES Emp,
                                      ON DELETE SET NULL)

3. Define the Dept relation in SQL so that every department is guaranteed to have a
manager.
CREATE TABLE Dept( did            INTEGER,
                                     budget      REAL,
                                     managerid INTEGER NOT NULL,
                                     PRIMARY KEY (did),
                                     FOREIGN KEY (managerid) REFERENCES Emp)

4. Write an SQL statement to add `John Doe' as an employee with eid = 101, age = 32
and salary = 15000.
INSERT 
INFO      Emp    (eid,ename,age,salary)
VALUES               (101,`John Doe',32,15000)

5. Write an SQL statement to give every employee a 10% raise.
UPDATE Emp E
SET E.salary = E.salary*1.10

6. Write an SQL statement to delete the `Toy' department. Given the referential integrity constraints you chose for this schema, explain what happens when this statement is executed.
DELETE
FROM   Dept  D
             WHERE D.dname='Toy'
Works中的did字段是一个外键,引用到Dept。这是选择的引用完整性约束。通过增加ON DELETE CASCADE,使得一个Dept被删除时,与之相关联的Works也将被删除。
查询结果:查找name为'Toy'的Dept记录,删除它,并且did为此记录的did的Works记录也一并删除。

Exercise 3.9 Consider the SQL query whose answer is shown in Figure 3.6.
1. Modify this query so that only the login column is included in the answer.
SELECT S.login
FROM   Student S
WHERE S.age<18
2. If the clause WHERE S.gpa >= 2 is added to the original query, what is the set of tuples
in the answer?
Madayan的记录被忽略,因为他的gpa<2。

Exercise 3.10 Explain why the addition of NOT NULL constraints to the SQL definition of the Manages relation (in Section 3.5.3) would not enforce the constraint that each department must have a manager. What, if anything, is achieved by requiring that the ssn field of Manages be non-null?
Manages的ssn非空限制只能保证每个Manages都有一个管理者,不能确保每个部门有一个管理者。

Exercise 3.11 Suppose that we have a ternary relationship R between entity sets A, B,and C such that A has a key constraint and total participation and B has a key constraint;these are the only constraints. A has attributes a1 and a2, with a1 being the key; B and C are similar. R has no descriptive attributes. Write SQL statements that create tables
corresponding to this information so as to capture as many of the constraints as possible. If you cannot capture some constraint, explain why.

CREATE TABLE  A( a1 CHAR(10),
                                 a2 CHAR(10),
                                 b1 CHAR(10),
                                 c1  CHAR(10),
                                 PRIMARY KEY (a1),
                                 UNIQUE (b1),
                                 FOREIGN KEY (b1) REFERNCES B,
                                 FOREIGN KEY (c1) REFERNCES C)
CREATE TABLE  B( b1 CHAR(10),
                                 b2 CHAR(10),
                                 PRIMARY KEY (b1))
CREATE TABLE  C( c1 CHAR(10),
                                 c2 CHAR(10),
                                 PRIMARY KEY (c1))

Exercise 3.12 Consider the scenario from Exercise 2.2 where you designed an ER diagram for a university database. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints, explain why.

1.CREATE TABLE  Teaches( ssn CHAR(10),
                                               semesterid CHAR(10),
                                               courseId INTEGER,
                                               PRIMARY KEY (ssn,semesterid,courseId),
                                               FOREIGN KEY (ssn) REFERNCES Professor,
                                               FOREIGN KEY (semesterid) REFERNCES Semester),
                                               FOREIGN KEY (courseId) REFERNCES Course))
CREATE TABLE  Semester(semesterid CHAR(10),
                                             PRIMARY KEY (semesterid))
Professor和Course类似于Semester。
2.CREATE TABLE  Teaches( ssn CHAR(10),
                                               semesterid CHAR(10),
                                               courseId INTEGER,
                                               PRIMARY KEY (ssn,courseId),
                                               FOREIGN KEY (ssn) REFERNCES Professor,
                                               FOREIGN KEY (courseId) REFERNCES Course))
Professor和Course保持不变。
3.不使用检查约束和断言的情况下,上一部分已经是最好的办法。只使用主键和外键约束时,参与约束无法被捕获,因为我们无法确保每个Professor对应一个Teaches。
4.CREATE TABLE  Professor teaches( ssn CHAR(10),
                                                               semesterid CHAR(10),
                                                               courseId INTEGER,
                                                               PRIMARY KEY (ssn),
                                                               FOREIGN KEY (courseId) REFERNCES Course))
CREATE TABLE  Course(courseId INTEGER,
                                          PRIMARY KEY (courseId))
不需要Teaches表。
5.CREATE TABLE  Professor teaches( ssn CHAR(10),
                                                              semesterid CHAR(10),
                                                              courseId INTEGER,
                                                              PRIMARY KEY (ssn),
                                                              FOREIGN KEY (courseId) REFERNCES Course))
不需要Course表,因为每一个Course对应一个教师,即对应一个Professor。
6.CREATE TABLE  Teaches( gid INTEGER,
                                               semester CHAR(10),
                                               courseId INTEGER,
                                               PRIMARY KEY (gid,courseId),
                                               FOREIGN KEY (gid) REFERNCES Group),
                                               FOREIGN KEY (courseId) REFERNCES Course))
CREATE TABLE  Member_of( ssn CHAR(10),
                                                 gid INTEGER,
                                                 PRIMARY KEY (ssn,gid),
                                                 FOREIGN KEY (ssn) REFERNCES Professor),
                                                 FOREIGN KEY (gid) REFERNCES Group))
CREATE TABLE  Course( courseId INTEGER,
                                          PRIMARY KEY (courseId))
CREATE TABLE  Group( gid INTEGER,
                                         PRIMARY KEY (gid))
CREATE TABLE  Professor( ssn CHAR(10),
                                              PRIMARY KEY (ssn))

Exercise 3.13 Consider the university database from Exercise 2.3 and the ER diagram that you designed. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints, explain why.

CREATE TABLE  Professors( profssn CHAR(10),
                                                name CHAR(64),
                                                age INTEGER,
                                                rank INTEGER,
                                                speciality CHAR(64),
                                                PRIMARY KEY (profssn))
CREATE TABLE  Depts( dno INTEGER,
                                        dname CHAR(64),
                                        office CHAR(10),
                                        PRIMARY KEY (dno))
CREATE TABLE  Runs( dno INTEGER,
                                       profssn CHAR(10),
                                       PRIMARY KEY (dno,profssn),
                                       FOREIGN KEY (profssn) REFERENCES Professors,
                                       FOREIGN KEY (dno) REFERENCES Depts)
CREATE TABLE  Work Dept( dno INTEGER,
                                                profssn CHAR(10),
                                                pctime INTEGER,
                                                PRIMARY KEY (dno,profssn),
                                                FOREIGN KEY (profssn) REFERENCES Professors,
                                                FOREIGN KEY (dno) REFERENCES Depts)
CREATE TABLE  Project( pid INTEGER,
                                          sponsor CHAR(32),
                                          start_date DATE,
                                          end_date DATE,
                                          budget FLOAT,
                                          PRIMARY KEY (pid))
CREATE TABLE  Graduate( gradssn CHAR(10),
                                              age INTEGER,
                                              deg_prog CHAR(32),
                                              name CHAR(64),
                                              major INTEGER,
                                              PRIMARY KEY (gradssn),
                                              FOREIGN KEY (major) REFERENCES Depts)
CREATE TABLE  Advisor(senior ssn CHAR(10),
                                          grad ssn CHAR(10),
                                          PRIMARY KEY (senior ssn,grad ssn),
                                          FOREIGN KEY (senior ssn) REFERENCES Graduates(grad ssn),
                                          FOREIGN KEY (grad ssn) REFERENCES Graduates)
CREATE TABLE  Manages(pid INTEGER,
                                            prof ssn CHAR(10),
                                            PRIMARY KEY (pid,prof ssn),
                                            FOREIGN KEY (prof ssn) REFERENCES Professors,
                                            FOREIGN KEY (pid) REFERENCES Projects)
CREATE TABLE  Work In(pid INTEGER,
                                          prof ssn CHAR(10),
                                          PRIMARY KEY (pid,prof ssn),
                                          FOREIGN KEY (prof ssn) REFERENCES Professors,
                                          FOREIGN KEY (pid) REFERENCES Projects)
CREATE TABLE  Supervised(pid INTEGER,
                                                prof ssn CHAR(10),
                                                grad ssn CHAR(10),
                                                PRIMARY KEY (pid,prof ssn,grad ssn),
                                                FOREIGN KEY (prof ssn) REFERENCES Professors,
                                                FOREIGN KEY (grad ssn) REFERENCES Graduates,
                                                FOREIGN KEY (pid) REFERENCES Projects)

Exercise 3.14 Consider the scenario from Exercise 2.4 where you designed an ER diagram for a company database. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints,explain why.

CREATE TABLE Employees( ssn CHAR(10),
                                                salary INTEGER,
                                                phone CHAR(13),
                                                PRIMARY KEY(ssn))
CREATE TABLE Departments( dno INTEGER,
                                                  dname CHAR(20),
                                                  budget INTEGER,
                                                  PRIMARY KEY(dno))
CREATE TABLE Work In( ssn CHAR(10),
                                          dno INTEGER,
                                          PRIMARY KEY(ssn,dno),
                                          FOREIGN KEY(ssn) REFERENCES Employees,
                                          FOREIGN KEY(dno) REFERENCES Departments)
CREATE TABLE Manages( ssn CHAR(10),
                                            dno INTEGER,
                                            PRIMARY KEY(dno),
                                            FOREIGN KEY(ssn) REFERENCES Employees,
                                            FOREIGN KEY(dno) REFERENCES Departments)
CREATE TABLE Dependent( ssn CHAR(10),
                                               name CHAR(10),
                                               age INTEGER,
                                               PRIMARY KEY(ssn,name),
                                               FOREIGN KEY(ssn) REFERENCES Employees ON DELETE CASCADE)

Exercise 3.15 Consider the Notown database from Exercise 2.5. You have decided to recommend that Notown use a relational database system to store company data. Show the SQL statements for creating relations corresponding to the entity sets and relationship sets in your design. Identify any constraints in the ER diagram that you are unable to capture in the SQL statements and briefly explain why you could not express them.

CREATE TABLE Musicians( ssn CHAR(10),
                                              name CHAR(30),
                                              PRIMARY KEY(ssn))
CREATE TABLE Instrument( instrId CHAR(10),
                                              dname CHAR(30),
                                              key CHAR(5),
                                              PRIMARY KEY(instrId))
CREATE TABLE Plays( instrId INTEGER,
                                      ssn CHAR(10),
                                      PRIMARY KEY(instrId,ssn),
                                      FOREIGN KEY(instrId) REFERENCES Instrument
                                      FOREIGN KEY(ssn) REFERENCES Musicians)
CREATE TABLE Songs Appears( songId INTEGER,
                                                      title CHAR(30),
                                                      suthor CHAR(30),
                                                      albumIdentifier INTEGER NOT NULL,
                                                      PRIMARY KEY(songId,ssn),
                                                      FOREIGN KEY(albumIdentifier) REFERENCES Album Producer)
CREATE TABLE Telephone Home( phone_no CHAR(11),
                                                         address CHAR(30),
                                                         PRIMARY KEY(phone_no),
                                                         FOREIGN KEY(address) REFERENCES Place)
CREATE TABLE Lives( phone_no CHAR(11),
                                      ssn CHAR(10),
                                      address CHAR(30),
                                      PRIMARY KEY(ssn,address),
                                      FOREIGN KEY(phone_no,address) REFERENCES Telephone Home,
                                      FOREIGN KEY(ssn) REFERENCES Musicians)
CREATE TABLE Place( address CHAR(30))
CREATE TABLE Perform( songId INTEGER,
                                           ssn CHAR(10),
                                           PRIMARY KEY(ssn,songId),
                                           FOREIGN KEY(songId) REFERENCES Songs,
                                           FOREIGN KEY(ssn) REFERENCES Musicians)
CREATE TABLE Album Producer( albumIdentifier INTEGER,
                                                        ssn CHAR(10),
                                                        copyrightDate DATE,
                                                        speed INTEGER,
                                                        title CHAR(30),
                                                        PRIMARY KEY(albumIdentifier),
                                                        FOREIGN KEY(ssn) REFERENCES Musicians)

Exercise 3.16 Translate your ER diagram from Exercise 2.6 into a relational schema, and show the SQL statements needed to create the relations, using only key and null constraints.If your translation cannot capture any constraints in the ER diagram, explain why.In Exercise 2.6, you also modified the ER diagram to include the constraint that tests on a plane must be conducted by a technician who is an expert on that model. Can you modify the SQL statements defining the relations obtained by mapping the ER diagram to check this constraint?

CREATE TABLE Expert( model no INTEGER,
                                        ssn CHAR(11),
                                        PRIMARY KEY(ssn,model no),
                                        FOREIGN KEY(ssn) REFERENCES Technicians,
                                        FOREIGN KEY(model no) REFERENCES Models)
参与约束无法捕获。
CREATE TABLE Models( model no INTEGER,
                                          capacity INTEGER,
                                          weight INTEGER,
                                          PRIMARY KEY(model no))
CREATE TABLE Employees( ssn CHAR(11),
                                                union men no INTEGER,
                                                PRIMARY KEY(ssn))
CREATE TABLE Technician emp( ssn CHAR(11),
                                                       name CHAR(20),
                                                       address CHAR(20),
                                                       phone no CHAR(14),
                                                       PRIMARY KEY(ssn),
                                                       FOREIGN KEY (ssn) REFERENCES Employees ON DELETE CASCADE)
CREATE TABLE Traffic control emp( ssn CHAR(11),
                                                            exam date DATE,
                                                            PRIMARY KEY(ssn),
                                                            FOREIGN KEY (ssn) REFERENCES Employees ON DELETE CASCADE)
CREATE TABLE Plane Type( reg_no INTEGER,
                                                model_no INTEGER,
                                                PRIMARY KEY(reg_no),
                                                FOREIGN KEY (model_no) REFERENCES Models)
CREATE TABLE Test info( FFA_no INTEGER,
                                           ssn CHAR(11),
                                           reg_no INTEGER,
                                           hours INTEGER,
                                           date DATE,
                                           score INTEGER,
                                           PRIMARY KEY(ssn,reg_no,FFA_no),
                                           FOREIGN KEY (reg_no) REFERENCES Plane Type,
                                           FOREIGN KEY (FAA_no) REFERENCES Test,
                                           FOREIGN KEY (ssn) REFERENCES Employees)
测试飞机的人必须是精通该飞机模型的专家:
CREATE TABLE Test info( FFA_no INTEGER,
                                           ssn CHAR(11),
                                           reg_no INTEGER,
                                           hours INTEGER,
                                           date DATE,
                                           score INTEGER,
                                           PRIMARY KEY(ssn,reg_no,FFA_no),
                                           FOREIGN KEY (reg_no) REFERENCES Plane Type,
                                           FOREIGN KEY (FAA_no) REFERENCES Test,
                                           FOREIGN KEY (ssn) REFERENCES Technician emp)
                                           CONSTRAINT MODEL
                                           CHECK( SELECT * FROM Expert,Type
                                                          WHERE Expert.ssn = ssn AND
                                                          Expert.model_no = Type.model_no AND
                                                          Type.reg_no = reg_no)

Exercise 3.17 Consider the ER diagram that you designed for the Prescriptions-R-X chain of pharmacies in Exercise 2.7. Define relations corresponding to the entity sets and relationship sets in your design using SQL.

CREATE TABLE Pri Phy Patient(ssn CHAR(11), 
                                                     name CHAR(20),
                                                     age INTEGER,
                                                     address CHAR(20),
                                                     physsn CHAR(11),
                                                     PRIMARYKEY(ssn), 
                                                     FOREIGNKEY(physsn) REFERENCES Doctor)
CREATE TABLE Prescription(ssn CHAR(11),
                                                physsn CHAR(11),
                                                date CHAR(11),
                                                quantity INTEGER,
                                                tradename CHAR(20),
                                                pharmid CHAR(11),
                                                PRIMARYKEY(ssn,physsn), 
                                                FOREIGNKEY(ssn) REFERENCES Patient,
                                                FOREIGNKEY(physsn) REFERENCES Doctor,
                                                FOREIGN KEY(tradename,pharmid) REFERENCES Make Drug)
CREATE TABLE Make Drug(tradenameCHAR(20), 
                                              pharmidCHAR(11), 
                                              PRIMARYKEY(tradename,pharmid), 
                                              FOREIGNKEY(tradename) REFERENCES Drug,
                                              FOREIGNKEY(pharmid) REFERENCES Pharmco)
CREATE TABLE Sell( price INTEGER,
                                   name CHAR(10),
                                   tradename CHAR(10), 
                                   PRIMARYKEY(name,tradename), 
                                   FOREIGNKEY(name) REFERENCES Pharmacy,
                                   FOREIGNKEY(tradename) REFERENCES Drug)
CREATE TABLE Contract( name CHAR(20),
                                           pharmid CHAR(11),
                                           startdate CHAR(11),
                                           enddate CHAR(11),
                                           text CHAR(10000),
                                           supervisor CHAR(20), 
                                           PRIMARYKEY(name,pharmid), 
                                           FOREIGNKEY(name) REFERENCES Pharmacy,
                                           FOREIGNKEY(pharmid) REFERENCES Pharmco)

Exercise 3.18 Write SQL statements to create the corresponding relations to the ER diagram you designed for Exercise 2.8. If your translation cannot capture any constraints in the ER diagram, explain why.

CREATE TABLE Customer( cust_id CHAR(20),
                                             name CHAR(20),
                                             address CHAR(32),
                                             amount INTEGER,
                                             PRIMARYKEY(cust_id))
CREATE TABLE Artist( name CHAR(20),
                                      birthplace DATE,
                                      age INTEGER,
                                      style CHAR(20),
                                      PRIMARYKEY(name))
CREATE TABLE Like_Artist( cust_id CHAR(20),
                                              name CHAR(20),
                                              PRIMARYKEY(cust_id,name))
                                              FOREIGNKEY(cust_id) REFERENCES Customer)
                                              FOREIGNKEY(name) REFERENCES Artist)
CREATE TABLE ArtWork Paints( title CHAR(20),
                                                     year INTEGER,
                                                     type CHAR(20),
                                                     price INTEGER,
                                                     name CHAR(20),
                                                     PRIMARYKEY(title),
                                                     FOREIGNKEY(name) REFERENCES Artist)
CREATE TABLE Classify( title CHAR(20),
                                          name CHAR(20),
                                          PRIMARYKEY(title,name),
                                          FOREIGNKEY(title) REFERENCES ArtWork Paints,
                                          FOREIGNKEY(name) REFERENCES Group)
CREATE TABLE Group( name CHAR(20),
                                        PRIMARYKEY(name))
CREATE TABLE Like_Group( cust_id CHAR(20),
                                                name CHAR(20),
                                                PRIMARYKEY(cust_id,name),
                                                FOREIGNKEY(cust_id) REFERENCES Customer,
                                                FOREIGNKEY(name) REFERENCES Group)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章