开发者

Oracle SQL Issue with Insert Statements

开发者 https://www.devze.com 2023-01-15 12:01 出处:网络
So I am a beginner to this, and trying to follow my professors instructions on how to complete this.They are as follows:

So I am a beginner to this, and trying to follow my professors instructions on how to complete this. They are as follows:

Consider the relational database described in Tables 1-3. The underlined attribute(s) is the primary key for a table. Use your Oracle account to create this database and to i开发者_如何转开发nsert the tuples listed in each table. Make sure you include the primary key and foreign key constraints. Put check constraints on the following three attributes. Rating (between 0 and 10), Lengith (greater than 0), ReleaseDate (after 1/1/1900).

Table 1: Theatres

Name (Underlined as Primary), City, State, Zip, Phone

Table 2: Movies

Title (Primary), Rating, Length, ReleaseDate

Table 3: ShownAt

TheatreName, MovieTitle (BOTH listed as Primary)

Here is how I have them declared:

CREATE TABLE Theatres (
Name   varchar2(50) not null,
City   varchar2(50) not null,
State   varchar2(50) not null,
Zip   number not null,
Phone   varchar2(50) not null,
CONSTRAINT name_pk PRIMARY KEY (Name)
);

create table Movies (
Title   varchar2(100),
Rating NUMBER CONSTRAINT Rating_CHK CHECK (Rating BETWEEN 0 AND 10),
Length NUMBER CONSTRAINT Length_CHK CHECK (Length > 0),
ReleaseDate date CONSTRAINT RDATE_CHK CHECK (ReleaseDate > to_date('1/January/1900', 'DD/MONTH/YYYY')),
CONSTRAINT title_pk PRIMARY KEY (Title)
);

create table ShownAt (
TheatreName varchar2(50),
MovieTitle varchar2(100),
CONSTRAINT moviet_fk FOREIGN KEY (MovieTitle) REFERENCES Movies(Title),
CONSTRAINT shown_pk PRIMARY KEY (TheatreName, MovieTitle)
);

This declares fine, and I get no errors. Below are my inserts (I gave one example insert for each table):

insert into theatres values ( 'Great Escape 14', 'Wilder', 'KY', '41076', '(859) 442-0000' );
insert into movies values ( 'The Expendables', '7.6', '103', '13 August 2010' );
insert into shownat values ( 'Great Escape 14', 'The Expendables' );

All inserts into theatres and movies go off without a hitch. For ShownAt, I get A LOT OF ERRORS, most of which look something like this:

Error starting at line 37 in command:
insert into shownat values ( 'Showcase Cinema De Lux Florence', 'The Pianist' )
Error report:
SQL Error: ORA-02291: integrity constraint (LANGB1.MOVIET_FK) violated - parent key not found

Meaning:

02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause:    A foreign key value has no matching primary key value.
*Action:   Delete the foreign key or add a matching primary key.

Any insert into ShownAt where the title is just one word (say the movie Up) works just fine, but anything with more than one word (like The Expendables) won't execute, and give the above error. ANY HELP would be much appreciated. I can post any other details that you need.


There is no technical reason why a primary key consisting of multiple words should fail when a single word key succeeeds.

Generally it is a good idea to avoid using long VARCHAR2 (or any string datatype) for primary key columns. This is because it is easier to introduce mistakes with mismatched case or extra spaces. The following are all distinct values: 'The Pianist', 'The pianist', 'The Pianist '.

So one explanation for the failure of some inserts into ShownAt is transcription errors: the additional complexity of the multi-word, mixed case key makes it more likely that you didn't get the keys exactly right in the failing statements.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号