Question

Learning Objectives: Learn to define constraints on tables Lean to define primary keys and foreign keys...

Learning Objectives:

Learn to define constraints on tables

Lean to define primary keys and foreign keys

Exercise                                                                               

In Lab 09, we will continue the practice of creating tables. You will need to write SQL statements for the tasks specified below. After you complete them, submit your SQL script (in one *.sql file) to Blackboard by 11:59pm Nov. 16 (Friday). Include your name as a comment in the SQL script submitted. The creation statement for each table is worth one point, thus 3 points in total for this practice. The instructor will grade them manually.

We will continue the example of we practiced in Lab 08 last week. If you have the database VRG in your MySQL server, drop it completely first before starting with this week’s exercise.

Re-create a database named VRG and set it as the default database. VRG is used for a small art gallery, the View Ridge Gallery, which sells contemporary European and North American fine arts. The database consists of three tables.

Create an ARTIST table that tracks the information about artists whose works are on sell by VRG;

Create a WORK table that tracks the information about each piece of art work ever owned by VRG;

The detail of those table requirements are as follows. You can reuse your script for Lab08 to save some typing.

ARTIST Table

ColumnName

Data Type

NULL Status

Column Constraints

ArtistID

Int

NOT NULL

This is a surrogate key: automatically increase by 1 for every new row, starting from 1 for the first row

LastName

Char(25)

NOT NULL

FirstName

Char(25)

NOT NULL

Nationality

Char(50)

NULL

The default value of nationality is “USA”

DateofBirth

Date

NULL

DateDeceased

Date

NULL

Table-Level Constraints

The combination of FirstName and LastName needs to be unique across rows in this table

DateofBirth needs to be earlier than DateDeceased (Hint: this needs a CHECK constraint. MySQL does not really support it, but let’s practice writing it once.)

Set ArtistID as the primary key of this table

WORK Table

ColumnName

Data Type

NULL Status

Column Constraints

WorkID

Int

NOT NULL

This is a surrogate key: automatically increase by 1 for every new row, starting from 1 for the first row

Title

Char(35)

NOT NULL

Copy

Char(12)

NOT NULL

Medium

Char(35)

NULL

The default value of nationality is “print”

Description

VarChar(1000)

NULL

ArtistID

Int

NOT NULL

Table-Level Constraints

The combination of Title and Copy needs to be unique across rows in this table

Set WorkID as the primary key of this table

Set ArtistID as the foreign key of this table, referring to the ArtistID column of the Artist table. Any changes (both updates and deletes) on the existing values of the ArtistID column of the Artist table will cascade to this table.

After creating the ARTIST and WORK tables, insert the following rows for each table. Pay attention that the new rows you add need to follow all the constraints we specified (or learn from the error messages MySQL reported if you don’t)

For the ARTIST table, insert the following rows by using INSERT statements:

LastName

FirstName

Nationality

DateofBirth

DateDeceased

Smith

James

1970-01-01

Henderson

Sue

1940-01-01

2017-01-01

Smith

James

1906-01-01

2000-10-4

Note: (1) Don’t insert any value if the value is not provided in the table above. After insertion, check how the surrogate key and the default value work.

          (2) When inserting a Date value, you also need to enclose the value with a pair of single quotation marks.

          (3) There will be a syntax error when inserting the last row. Think why this error happens, and then fix it by changing the value of Firstname in the to-be-inserted row to something else.

For the WORK table, insert the following rows by using INSERT statements:

Title

Copy

Medium

Description

ArtistID

Lily Pond

the 74th

2

Sunflower

the 1st

Canvas

2

Random Error

the 1st

5

Note: (1) Don’t insert any value if the value is not provided in the table above. After insertion, check how the surrogate key and the default value work.

          (2) There will be a syntax error when inserting the last row. Think why this error happens, and abandon this insertion.

Update the ArtistID in one row of the Artist table, and check how the data of the WORK table change

Write an UPDATE statement to change the ArtistID from 2 to 138 for the ARTIST table. After update, check what changes have happened to the data in both ARTIST and WORK table.

Delete a row in the Artist table, and check how the data of the WORK table change

Write a DELETE statement to delete rows with an ArtistID as 138 from the ARTIST table. After deletion, check what changes have happened to the data in both ARTIST and WORK table.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*-------- Drop Databse ----------------------------*/

drop database VRG

/*--------------------------------------------------------------*/

/*-------- Create Databse ----------------------------*/

create database VRG

/*--------------------------------------------------------------*/

/*-------- Creating Table Artist ----------------------------*/

CREATE TABLE Artist (

ArtistID INT IDENTITY(0,1) NOT NULL,

LastName char(25) NOT NULL,

FirstName char(25)NOT NULL,

Nationality char(50) default 'USA',

DateofBirth date,

DateDeceased date,

PRIMARY KEY (ArtistID),

CONSTRAINT LF_Person UNIQUE (FirstName,LastName),

CHECK (DateofBirth<DateDeceased)

);

/*--------------------------------------------------------------*/

/*-------- Creating Table Work ----------------------------*/

CREATE TABLE WORK (

WorkID INT IDENTITY(0,1) NOT NULL,

Title char(35) NOT NULL,

Copy char(12)NOT NULL,

Medium char(35) default 'print',

Description varchar(1000),

ArtistID INT,

CONSTRAINT TC UNIQUE (Title,Copy),

PRIMARY KEY (WorkID),

FOREIGN KEY (ArtistID) REFERENCES Artist(ArtistID)

);

/*--------------------------------------------------------------*/

/*-------- Insert In Artist ----------------------------*/

insert into Artist(LastName,FirstName,Nationality,DateofBirth,DateDeceased) values('Smith','James',

1970-01-01),('Henderson','Sue',1940-01-01,2017-01-01),('Smith','James',1906-01-01,2000-10-4);

/*--------------------------------------------------------------*/

/*-------- Insert In WORK ----------------------------*/

insert into WORK (Title,Copy,Medium,Description,ArtistID) values('Lily Pond','

the 74th',2),('Sunflower','the 1st','Canvas',2),('Random Error',5);

/*--------------------------------------------------------------*/

Add a comment
Know the answer?
Add Answer to:
Learning Objectives: Learn to define constraints on tables Lean to define primary keys and foreign keys...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 16. The primary key is selected from the: Group of answer choices composite keys. foreign keys....

    16. The primary key is selected from the: Group of answer choices composite keys. foreign keys. candidate keys. surrogate keys. 17. "Delete Table" statement is used to remove a table definition and all the data, indexes, triggers, constraints and permission specifications for that table. Group of answer choices True False 18. Which of the following commands always affect one row at a time when it runs? Group of answer choices Drop Insert Delete Update 19. Data stored as an image...

  • DROP TABLE EMPLOYEE; DROP TABLE JOB; DROP TABLE EMP; DROP TABLE EMP_1; DROP TABLE EMP_2; CREATE...

    DROP TABLE EMPLOYEE; DROP TABLE JOB; DROP TABLE EMP; DROP TABLE EMP_1; DROP TABLE EMP_2; CREATE TABLE JOB(JOB_CODE CHAR (3) PRIMARY KEY, JOB_DESCRIPTION VARCHAR (20) NOT NULL,JOB_CHG_HOUR NUMBER (5,2) NOT NULL,JOB_LAST_UPDATE DATE NOT NULL); INSERT INTO JOB VALUES('500','Programmer','35.75','20-Nov-2017'); INSERT INTO JOB VALUES('501','System Analyst','96.75','20-Nov-2017'); INSERT INTO JOB VALUES('502','Database Designer','125.00','24-Mar-2018'); CREATE TABLE EMPLOYEE(EMP_NUM CHAR (3) PRIMARY KEY,EMP_LNAME VARCHAR (15) NOT NULL,EMP_FNAME VARCHAR (15) NOT NULL, EMP_INITIAL CHAR (1),EMP_HIREDATE DATE NOT NULL,JOB_CODE CHAR (3), EMP_YEARS NUMBER (2),FOREIGN KEY (JOB_CODE) REFERENCES JOB (JOB_CODE)); INSERT...

  • Step 1: Create table audits via triggers The system must log any insertion, deletion, or updates...

    Step 1: Create table audits via triggers The system must log any insertion, deletion, or updates to the following tables: • Employee table (project 1) create table Employee ( empNumber char(8) not null, firstName varchar(25) null, lastName varchar(25) null, ssn char(9) null, address varchar(50) null, state char(2) null, zip char(5) null, jobCode char(4) null, dateOfBirth date null, certification bit null, salary money null, constraint PK_EMP PRIMARY KEY(empNumber), constraint EMP_STATECHECK CHECK(state in ('CA','FL')) ) GO • Job table (project 1) create...

  • For this set of Review Questions, we will create and use a database for the Wedgewood...

    For this set of Review Questions, we will create and use a database for the Wedgewood Pacific Corporation (WPC) that is similar to the Microsoft Access database we created and used in Chapters 1 and 2. Founded in 1957 in Seattle, Washington, WPC has grown into an internationally recognized organization. The company is located in two buildings. One building houses the Administration, Accounting, Finance, and Human Resources departments, and the second houses the Production, Marketing, and Information Systems departments. The...

  • database and sql problme THE QUESTIONS 3, 4,5 and 6 REFER TO THE RELATIONAL TABLES LISTED...

    database and sql problme THE QUESTIONS 3, 4,5 and 6 REFER TO THE RELATIONAL TABLES LISTED BELOW CREATE TABLE Department ( DECIMAL(5) VARCHAR(30) CHAR(5) DATE NOT NULL NOT NULL NOT NULL * Department number /*Department name * Department manager number */ /Manager start date DNumber DName Manager MSDate CONSTRAINT Department_PK PRIMARY KEY(DNumber) CONSTRAINT Department_CK UNIQUE(DName) CREATE TABLE DeptLocation DECIMAL(5) VARCHAR(50) NOT NULL NOT NULL DNumber * Department number */ * Department location */ Address CONSTRAINT DeptLocation_PK PRIMARY KEY(DNumber, Address) CONSTRAINT...

  • Hi i need Help. i use MYSQL command line. THE QUESTION ARE BELOW please check the...

    Hi i need Help. i use MYSQL command line. THE QUESTION ARE BELOW please check the answer before you submit because sometimes query gives error thank you For Full database of SQL you can DOWNLOAD from this link: https://drive.google.com/file/d/1xh1TcBfMtvKoxvJr7Csgnts68fF53Q1t/view?usp=sharing ------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- What are the total sales for each year. Note that you will need to use the YEAR function. Order by year with the newest year first. ------------------------------------------------------- How many employees have no customers? ------------------------------------------------------------------ List the total sales for...

  • -- echo Building demo tables. Please wait. DROP TABLE EMP CASCADE CONSTRAINTS; DROP TABLE DEPT; DROP...

    -- echo Building demo tables. Please wait. DROP TABLE EMP CASCADE CONSTRAINTS; DROP TABLE DEPT; DROP TABLE SALGRADE; DROP TABLE Prod CASCADE CONSTRAINTS; DROP TABLE Vend; CREATE TABLE DEPT ( DEPTNO NUMBER(2) NOT NULL, DNAME CHAR(14), LOC CHAR(13), CONSTRAINT DEPT_PRIMARY_KEY PRIMARY KEY (DEPTNO)); INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK'); INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS'); INSERT INTO DEPT VALUES (30,'SALES','CHICAGO'); INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON'); CREATE TABLE EMP ( EMPNO NUMBER(4) NOT NULL, ENAME CHAR(10), JOB CHAR(9), MGR NUMBER(4) CONSTRAINT EMP_SELF_KEY...

  • I need help with the following SQL query for a company database (script given below). The...

    I need help with the following SQL query for a company database (script given below). The name of the Department. The number of employees working in that department. The number of different projects controlled by this department. The name of the project controlled by this department that has the maximum number of employees of the company working on it. The number of the above project. The cumulative sum of the number of employees of the company working on the projects...

  • please write a file with the commands attached SQL commans For this assignment, submit a text...

    please write a file with the commands attached SQL commans For this assignment, submit a text file with the commands that you entered into your MySQL database on burrow to do the following: 1. (25 points) Create a table called Singer, when described it should have the following values: | Field I Type I Null Key Default l Extra | SingerID int (11) | FirstName | varchar (30) YESI I LastName varchar (30) YES I | Label | Salary I...

  • drop table department cascade constraints; create table department ( Dname   varchar2(15)   not null, Dnumber int   not...

    drop table department cascade constraints; create table department ( Dname   varchar2(15)   not null, Dnumber int   not null, Mgr_ssn   char(9)   not null, mgr_start_date   Date, primary key (Dnumber), Unique    (Dname)); insert into DEPARTMENT values ('Research', '5', '333445555', '22-May-1988'); insert into DEPARTMENT values ('Administration', '4', '987654321', '01-Jan-1995'); insert into DEPARTMENT values ('Headquarters', '1', '888665555', '19-Jun-1981'); drop table employee cascade constraints; create table employee ( Fname   varchar2(15) not null, Minit   char(1), Lname   varchar2(15) not null, Ssn   char(9), Bdate   date, Address   varchar2(30), Sex   char(1),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT