Question: SQL CHECK CONSTRAINT AND TEST CASE... SQL Query Here are the tables: CREATE TABLE Movies( movieID INT, name VARCHAR(30) NOT NULL, year INT, rating CHAR(1),

SQL CHECK CONSTRAINT AND TEST CASE...

SQL Query

Here are the tables:

CREATE TABLE Movies(

movieID INT,

name VARCHAR(30) NOT NULL,

year INT,

rating CHAR(1),

length INT,

totalEarned NUMERIC(7,2),

PRIMARY KEY(movieID),

UNIQUE(name, year)

);

CREATE TABLE Showings(

theaterID INT,

showingDate DATE,

startTime TIME,

movieID INT,

priceCode CHAR(1),

PRIMARY KEY(theaterID, showingDate, startTime),

FOREIGN KEY(theaterID) REFERENCES Theaters,

FOREIGN KEY(movieID) REFERENCES Movies

);

CREATE TABLE Tickets(

theaterID INT,

seatNum INT,

showingDate DATE,

startTime TIME,

customerID INT,

ticketPrice NUMERIC(4,2),

PRIMARY KEY(theaterID, seatNum, showingDate, startTime)

);

the tasks are to write a check constraint as such:

3. In Showings, if movieID is not NULL, then priceCode must also not be NULL.

AND then i need to write an update statement that makes sure it fails per the constraint.

i've written:

ALTER TABLE Showings ADD CHECK ((movieID <> NULL) OR (priceCode <> NULL)); for the constraint

and

UPDATE Showings

SET priceCode = 1

WHERE movieID is not NULL; for the failing test.

i believe this is right, but the update statement is not failing like it is supposed to.

can someone see if i'm doing anything wrong or please explain how to correctly write it?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!