Question

SQL CHECK CONSTRAINT AND TEST CASE... SQL Query Here are the tables: CREATE TABLE Movies(     movieID...

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?

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

Since, this is a column dependency constraint, it will be done by dealing cases:

This means, if movieID is not NULL, then priceCode is not NULL. But if movieID is NULL, then priceCode can either be NULL or not NULL.

So the constraint goes like this,

CONSTRAINT CHK_Showings CHECK(((movieID is NULL) and ((priceCode is not NULL) or(priceCode is NULL))) or ((movieID is not null) and (priceCode is not null)))

**either use alter table for adding this constraint, or rebuild this table using this constraint**

And also, the test case used is incorrect, as it goes in favor of what we have done. We have to check that if movieID is not NULL then price code should also be NOT NULL. The failing test case would be the following, which tries updating priceCode to NULL, in records where movieID is not NULL, and then shows error.

UPDATE Showings
SET priceCode = NULL
WHERE movieID is not NULL;

Error Message:

Add a comment
Know the answer?
Add Answer to:
SQL CHECK CONSTRAINT AND TEST CASE... SQL Query Here are the tables: CREATE TABLE Movies(     movieID...
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
  • WRITING POSTGRESQL QUERIES RELATIONS: CREATE TABLE Movies(    movieID INT,    year INT UNIQUE,    rating...

    WRITING POSTGRESQL QUERIES RELATIONS: CREATE TABLE Movies(    movieID INT,    year INT UNIQUE,    rating CHAR(1),    length INT,    totalEarned NUMERIC(7,2),    PRIMARY KEY(movieID) ); CREATE TABLE Theaters(    theaterID INT,    address VARCHAR(40) UNIQUE,    numSeats INT NOT NULL,    PRIMARY KEY(theaterID) ); CREATE TABLE TheaterSeats(    theaterID INT,    seatNum INT,    brokenSeat BOOLEAN NOT NULL,    PRIMARY KEY(theaterID, seatNum),    FOREIGN KEY(theaterID) REFERENCES Theaters ); CREATE TABLE Showings(    theaterID INT,    showingDate DATE,   ...

  • Utilize the JigSaw SQL file below to create a Star Schema diagram. Remember, to create a...

    Utilize the JigSaw SQL file below to create a Star Schema diagram. Remember, to create a Star Schema from a normalized data model, you will need to denormalize the data model into fact and dimension tables. The diagram should contain all of the facts and dimension tables necessary to integrate the JigSaw operational database into a data warehouse. Write a brief paper describing the challenges you experienced in completing this assignment. -- CREATE DATABASE js; CREATE TABLE buy_methods ( buy_code...

  • CREATE TABLE Gender ( gender CHAR(1), description VARCHAR(10), PRIMARY KEY (gender) ); CREATE TABLE People (...

    CREATE TABLE Gender ( gender CHAR(1), description VARCHAR(10), PRIMARY KEY (gender) ); CREATE TABLE People ( ID INT, name VARCHAR(50), gender CHAR(1), height FLOAT, PRIMARY KEY (ID), FOREIGN KEY (gender) REFERENCES Gender (gender) ); CREATE TABLE Sports ( ID INT, name VARCHAR(50), record FLOAT, PRIMARY KEY (ID), UNIQUE (name) ); CREATE TABLE Competitions ( ID INT, place VARCHAR(50), held DATE, PRIMARY KEY (ID) ); CREATE TABLE Results ( peopleID INT NOT NULL, competitionID INT NOT NULL, sportID INT NOT NULL,...

  • SQL Query Question: I have a database with the tables below, data has also been aded...

    SQL Query Question: I have a database with the tables below, data has also been aded into these tables, I have 2 tasks: 1. Add a column to a relational table POSITIONS to store information about the total number of skills needed by each advertised position. A name of the column is up to you. Assume that no more than 9 skills are needed for each position. Next, use a single UPDATE statement to set the values in the new...

  • The following SQL DDL script creates a database for a social network application. create table userProfile(...

    The following SQL DDL script creates a database for a social network application. create table userProfile( id char(10) primary key, firstName varchar(20), lastName varchar(20), dob date, email varchar(30) ); create table foaf( userid char(10), friendID char(10), timeEstablished date, constraint pk primary key(userid, friendID), constraint fk1 foreign key(userid) references userProfile(id), constraint fk2 foreign key(friendID) references userProfile(id) ); create table activity( actID char(20) primary key, topic varchar(20), description varchar(100), location varchar(20), ActivityDate date, hostUser char(10), foreign key(hostUser) references userProfile(id) ); create table...

  • Create a new database and execute the code below in SQL Server’s query window to create...

    Create a new database and execute the code below in SQL Server’s query window to create the database tables. CREATE TABLE PhysicianSpecialties (SpecialtyID integer, SpecialtyName varchar(50), CONSTRAINT PK_PhysicianSpecialties PRIMARY KEY (SpecialtyID)) go CREATE TABLE ZipCodes (ZipCode varchar(10), City varchar(50), State varchar(2), CONSTRAINT PK_ZipCodes PRIMARY KEY (ZipCode)) go CREATE TABLE PhysicianPractices (PracticeID integer, PracticeName varchar(50), Address_Line1 varchar(50), Address_Line2 varchar(50), ZipCode varchar(10), Phone varchar(14), Fax varchar(14), WebsiteURL varchar(50), CONSTRAINT PK_PhysicianPractices PRIMARY KEY (PracticeID), CONSTRAINT FK_PhysicianPractices_ZipCodes FOREIGN KEY (ZipCode) REFERENCES Zipcodes) go CREATE...

  • CREATE TABLE GLAccounts (    AccountNo INT,    AccountDescription VARCHAR(50),    --    CONSTRAINT GLAccounts_PK PRIMARY...

    CREATE TABLE GLAccounts (    AccountNo INT,    AccountDescription VARCHAR(50),    --    CONSTRAINT GLAccounts_PK PRIMARY KEY(AccountNo),    --    CONSTRAINT AccountDescription_NULL CHECK(AccountDescription IS NOT NULL),    --    CONSTRAINT AccountDescription_Zero_LEN CHECK(LEN(AccountDescription)>0) ); GO ----------------------------------------------------------------------------- CREATE TABLE Terms (    TermsID INT,    TermsDescription VARCHAR(50),    TermsDueDays SMALLINT,    --    CONSTRAINT PK_Terms PRIMARY KEY(TermsID),    --    CONSTRAINT TermsDescription_NULL    CHECK(TermsDescription IS NOT NULL),    CONSTRAINT TermsDueDays_NULL        CHECK(TermsDueDays IS NOT NULL),    --    CONSTRAINT TermsDescription_zero_LEN   ...

  • Write an SQL query to return the users who posted the most number of jokes on...

    Write an SQL query to return the users who posted the most number of jokes on 1/1/2019. Database: create table User( userid varchar(30) not null, password varchar(30), firstname varchar(30), lastname varchar(50), email varchar(50), gender char(1), age integer, banned boolean, primary key(userid), unique(email)); create table MyFriends( userid varchar(30), friendid varchar(30), primary key(userid,friendid), foreign key(userid) references User, foreign key(friendid) references User(userid)); Create table Jokes( Jokeid integer, title varchar(100), description varchar(255), authorid varchar(30) not null, primary key(jokeid), posttime Datetime, foreign key(authorid) references User(userid));...

  • MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects...

    MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects 2.List the project with the most hours SCRIPT TO BUILD DATABASE create table department ( dept_ID int not null , dept_name char(50) NOT NULL, manager_ID int not null, manager_start_date date not null, constraint Dept_PK primary key (dept_ID), constraint D_Name_AK unique (dept_name) ); insert into department values(1,'abc',1,'2019-01-08'); insert into department values(2,'abc2',2,'2019-01-08'); insert into department values(3,'abc3',2,'2019-01-08'); insert into department values(4,'abc4',2,'2019-01-08'); /*project table done*/ create table project...

  • NEED HELP IN INSERT STATEMENTS FOR THE TABLES CAN YOU PLEASE ADD SOME INSERT STATEMENTS FOR...

    NEED HELP IN INSERT STATEMENTS FOR THE TABLES CAN YOU PLEASE ADD SOME INSERT STATEMENTS FOR A COMPANY SCHEMA SCRIPT. PLEASE ADN THANK YOU DROP TABLE dependent; DROP TABLE works_on; DROP TABLE project; DROP TABLE dept_locations; DROP TABLE department; DROP TABLE employee; CREATE TABLE employee ( fname varchar(15) not null, minit varchar(1), lname varchar(15) not null, ssn char(9), bdate date, address varchar(50), sex char, salary numeric(10,2), superssn char(9), dno numeric, primary key (ssn), foreign key (superssn) references employee(ssn) ); CREATE...

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