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)

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