cant reference sport CHAR(6) to other column error - sql

As the title says, for some reason it gives me the error that the sport variable cant reference to another column when I am not even referencing it, any reason for this?
CREATE TABLE club_rates
(club_id NUMBER(4)
CONSTRAINT rates_club_fk REFERENCES club_subscriptiontypes(club_id,subscriptiontype)
,sport CHAR(6)
,subscriptiontype CHAR(11)
,subscription_startdate DATE
,subscription_rate_existing NUMBER(2)
,subscription_rate_new NUMBER(2)
,subscription_enddate DATE
,registration_startdate DATE
,registration_enddate DATE,
CONSTRAINT rates_clubsportsub_pk PRIMARY KEY (club_id, sport, subscriptiontype, subscription_startdate)
);

Assuming you have the table:
CREATE TABLE club_subscriptiontypes(
club_id NUMBER(4),
subscriptiontype CHAR(11),
PRIMARY KEY (club_id,subscriptiontype)
);
Then you do not want to declare a composite primary key column inline against a single column or you get the error:
ORA-02256: number of referencing columns must match referenced columns
Instead, declare it out-of-line at the end of the statement:
CREATE TABLE club_rates(
club_id NUMBER(4)
, sport CHAR(6)
, subscriptiontype CHAR(11)
, subscription_startdate DATE
, subscription_rate_existing NUMBER(2)
, subscription_rate_new NUMBER(2)
, subscription_enddate DATE
, registration_startdate DATE
, registration_enddate DATE
, CONSTRAINT rates_club_fk
FOREIGN KEY (club_id, subscriptiontype)
REFERENCES club_subscriptiontypes(club_id,subscriptiontype)
, CONSTRAINT rates_clubsportsub_pk
PRIMARY KEY (club_id, sport, subscriptiontype, subscription_startdate)
);
As an aside, you probably don't want to use fixed-length CHAR strings and, instead, want variable-length VARCHAR2 strings.
db<>fiddle here

Related

ORA-00907: "missing right parenthesis"

First of all, I apologize for asking the same question again but the information on the old topics did not work in my oraclesql code and secondly there may be some syntax errors, I learned mysql in school last semester, but now we are learning oraclesql with old mysql information in the lesson, so i am a bit confused.
So my question is i want to create 3 related table (movie-casts-actor) but i am getting missing right parenthesis. trying to connect casts table to movie table with foreign key and actor table to casts table. what am i doing wrong? how spagetti is it?
CREATE TABLE Movie (
movie_id NUMBER(7) NOT NULL,
movie_name varchar2(50) NOT NULL ,
movie_director varchar2(50) NOT NULL ,
movie_year INT(4) NOT NULL ,
movie_duration INT(3) ,
movie_language varchar2(15) ,
movie_rating number(4,2) ,
PRIMARY KEY(movie_id),
CONSTRAINT is_unique UNIQUE(movie_id),
CONSTRAINT movie_id_checker CHECK(movie_id>0 and movie_id<=9999999)
);
CREATE TABLE Casts (
movie_id_fk INT(7) FOREIGN KEY REFERENCES Movie(movie_id) ,
cast_id INT(7) NOT NULL,
actor_fullname varchar2(50) NOT NULL ,
actor_role varchar2(50) ,
PRIMARY KEY(cast_id),
CONSTRAINT is_unique UNIQUE(cast_id),
CONSTRAINT cast_id_checker CHECK(cast_id>0 and cast_id<=9999999)
);
CREATE TABLE Actor (
cast_id_fk INT(7) FOREIGN KEY REFERENCES Casts(cast_id) ,
actor_id INT(7) NOT NULL,
actor_name CHAR(30) ,
actor_surname CHAR(25) ,
actor_gender CHAR(5) ,
actor_age CHAR(3) CONSTRAINT real_age_check CHECK(actor_age>0 AND actor_age<=150),
PRIMARY KEY(actor_id),
CONSTRAINT is_unique UNIQUE(actor_id),
CONSTRAINT actor_id_checker CHECK(actor_id>0 and actor_id<=9999999)
)
The errors are:
INT or NUMBER(4,0) and not INT(4)
You cannot have a unique key and a primary key on the same column.
An inline foreign key just need the REFERENCES keyword and does not need FOREIGN KEY.
Other issues:
You probably don't want actor name/surname as fixed-length CHAR strings and want to us variable-length VARCHAR2.
You probably want gender to be a code from a fixed list (which can be as long or as short as you find appropriate to describe the actors) rather than as a string.
You probably don't want to have an AGE column as that will be out of date as soon as the first birthday of an actor occurs; instead have a DATE_OF_BIRTH column that is a DATE data type and then you can calculate the age as and when necessary.
Using the table name as a prefix for every column is a waste of key strokes; you would be better to just name the columns for what they are without the prefix. Similar with fk as a suffix.
If you are using a NUMBER(7,0) for id values then you don't need to check that it is less than or equal to 9999999 as it is impossible to be a greater value; however, you can have zero or negative values so the check constraint for the lower bound may still be valid.
If you are using Oracle 12c or later then you should probably be using an IDENTITY column for the id values unless you are taking the id values from a 3rd party.
CREATE TABLE Movie (
id NUMBER(7,0) NOT NULL,
name varchar2(50) NOT NULL ,
director varchar2(50) NOT NULL ,
year NUMBER(4,0) NOT NULL ,
duration NUMBER(3,0),
language varchar2(15) ,
rating number(4,2) ,
PRIMARY KEY(id),
CONSTRAINT movie_id_checker CHECK(id>0)
);
CREATE TABLE Casts (
movie_id INT REFERENCES Movie(id) ,
id NUMBER(7,0) NOT NULL,
fullname varchar2(50) NOT NULL ,
role varchar2(50) ,
PRIMARY KEY(id),
CONSTRAINT cast_id_checker CHECK(id>0)
);
CREATE TABLE Actor (
cast_id NUMBER(7,0) REFERENCES Casts(id) ,
id NUMBER(7,0) NOT NULL,
name VARCHAR2(30) ,
surname VARCHAR2(25) ,
gender CHAR(1)
CHECK ( gender IN ( 'M', 'F', 'X', 'Y', 'Z' ) ),
date_of_birth DATE
CONSTRAINT real_age_check CHECK(date_of_birth >= DATE '1870-01-01' ),
PRIMARY KEY(id),
CONSTRAINT actor_id_checker CHECK(id>0)
);
db<>fiddle here

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

Attempting to create a table with composite keys and date and time data types. Only get errors

OK so here is the table and data types I am attempting to create in sql developer:
Table_Name interview
Field_Name e_number, pos_id, date, time, awarded
Data_Type varchar2(9), varchar2(15), date, number(9), varchar2(3)
Nulls_Allowed
Primary_Key y, y, n, n, n
Unique y, y, n, n, n
Foreign_Key
Comments
Here is the sql I am trying to use:
CREATE TABLE interview(
e_number VARCHAR2(9) NOT NULL UNIQUE,
pos_id VARCHAR2(15) NOT NULL UNIQUE,
date DATE NOT NULL,
time NUMBER(8) NOT NULL,
awarded VARCHAR2(3),
CONSTRAINT pk_interview PRIMARY KEY (e_number, pos_id),
CONSTRAINT fk_e_number FOREIGN KEY (e_number) REFERENCES student
(e_number),
CONSTRAINT fk_pos_id FOREIGN KEY (pos_id) REFERENCES position
(pos_id)
Can someone to tell me where I have "invalid identifier"? Cause I am completely lost.
CREATE TABLE interview(
e_number VARCHAR2(9) NOT NULL UNIQUE,
pos_id VARCHAR2(15) NOT NULL UNIQUE,
"date" DATE NOT NULL,
"time" NUMBER(8) NOT NULL,
awarded VARCHAR2(3),
CONSTRAINT pk_interview PRIMARY KEY (e_number, pos_id),
CONSTRAINT fk_e_number FOREIGN KEY (e_number) REFERENCES student (e_number),
CONSTRAINT fk_pos_id FOREIGN KEY (pos_id) REFERENCES position (pos_id)
);
As suggested by #Remy_Lebeau, #CLifford, and #amdixon
date and time are reserved words (see this post), so enclose them in quotations. Also, add right parentheses and semicolon to close your table declaration.

I cannot create a simple table

I try to Write the SQL code to create the table named ‘EMP_1’. This table is a subset of the EMPLOYEE table, and the structure of the table is summarized as shown below.
This is the information:
Attribute Name Data Type Remarks
EMP_NUM CHAR(3) PK
EMP_LNAME VARCHAR(15) Not Null
EMP_FNAME VARCHAR(15) Not Null
EMP_INITIAL CHAR(1)
EMP_HIREDATE DATE
JOB_CODE CHAR(3) FK (from JOB table)
My code:
CREATE TABLE EMP_1
(
EMP_NUM CHAR(3) PRIMARY KEY,
EMP_LNAME VARCHAR(15) Not Null,
EMP_FNAME VARCHAR(15) Not Null,
EMP_INITIAL CHAR(1) ,
EMP_HIREDATE DATETIME,
JOB_CODE CHAR(3) FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);
I keep getting CONSTRAINT error
I think you might be missing a comma before the constraint. This worked when I tried it:
CREATE TABLE EMP_1
(
EMP_NUM CHAR(3) PRIMARY KEY,
EMP_LNAME VARCHAR(15) Not Null,
EMP_FNAME VARCHAR(15) Not Null,
EMP_INITIAL CHAR(1),
EMP_HIREDATE DATETIME,
JOB_CODE CHAR(3),
CONSTRAINT FK_JOBS FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);
Make sure your PRIMARY KEY and FOREIGN KEY have the same DATA TYPE.
I've not scrpited a FK for a while, but my recollection is that you can EITHER use:
JOB_CODE CHAR(3) FOREIGN KEY REFERENCES JOB(JOB_CODE)
which will create an FK named JOB_CODE,
OR:
split the line so you create the field and add the constraint separately as per JPW's response.
Your original code tries to combine the 2 approaches which will throw the error you describe.

Oracle SQL : Why cant each foreign key from separate tables reference to a single primary key?

create table Product(
Prodid number(5) primary key
, Prodesc varchar(20)
, Price number(6,2)
, Stock number (6)
);
This foreign key refers to a prodid of product table:
create table Purchase(
Purid number(9) primary key
, Proid number(5)
, CONSTRAINT fk_Product FOREIGN KEY(Proid) REFERENCES Product(Prodid)
, qty number(6)
, supname char(15)
);
This foreign key also refers to a prodid of product table:
create table Sales(
Saleid number(9) primary key
, Proid number(5)
, CONSTRAINT fk_Product FOREIGN KEY(Proid) REFERENCES Product(Proid)
, qty number(6)
, custname char(15)
);
But I am getting an error. "name already used by a constraint." . Any solution to overcome this?. I want those two table's(Purchase & Sales) foreign key reference to the same primary key of Product table.
The problem, as #OldProgrammer and others suggest is that you're trying to use the same constraint name in both cases. You'd need to use different constraint names. You'll probably want to use some sort of naming convention for your keys-- I prefer one that uses both the parent and the child in the name. So I'd do something like
create table Purchase(
Purid number(9) primary key
, Proid number(5)
, CONSTRAINT fk_purchase_product FOREIGN KEY(Proid) REFERENCES Product(Prodid)
, qty number(6)
, supname char(15)
);
create table Sales(
Saleid number(9) primary key
, Proid number(5)
, CONSTRAINT fk_sales_product FOREIGN KEY(Proid) REFERENCES Product(Proid)
, qty number(6)
, custname char(15)
);
Not related to your error but I would be highly dubious that any column should ever be declared as a char(15). varchar2(15) almost certainly makes more sense-- it is highly unlikely that you want your names space padded to 15 bytes or that you want to potentially use char comparison semantics in your code.