Missing left parenthesis - oracle

Missing left parenthesis on line 9..can't see the error!
create table EDITIONS(
ID number NOT NULL,
ID_VOLUME number,
Publishing varchar2(20),
Year number(4),
Language varchar2(10),
Pages number(4),
CONSTRAINT pk_editions PRIMARY KEY,
constraint fk_editions_volume foreign key (id_volume)
references volume(id),
constraint editions_publishing,
constraint editions_year,
constraint editions_language,
constraint editions_pages,
);

Try something like this:
create table EDITIONS (
ID number NOT NULL primary key
ID_VOLUME number,
Publishing varchar2(20),
Year number(4),
Language varchar2(10),
Pages number(4),
constraint fk_editions_volume foreign key (id_volume) references volume(id)
);
I assume you are using Oracle because of the varchar2(). You should tag questions with the correct database.

Related

ORA-02256: number of referencing columns must match referenced [duplicate]

This question already has an answer here:
ORA-02256: referencing foreign key
(1 answer)
Closed last year.
CREATE TABLE Goft_ForeEver_cus(
customer_Id VARCHAR(10),
first_Name VARCHAR2(20) CONSTRAINT sys_cus_fName_nn NOT NULL,
last_Name VARCHAR2(20) CONSTRAINT sys_cus_lName_nn NOT NULL,
girt_Card_Amount NUMBER(5,2) CONSTRAINT sys_cus_gca_nn NOT NULL,
email VARCHAR2(30) CONSTRAINT sys_cus_email_ck CHECK (email ='%[^a-z,0-9,#,.,_,-]%' ) CONSTRAINT sys_cus_email_nn NOT NULL,
CONSTRAINT sys_cus_pk PRIMARY KEY(customer_Id)
);
ALTER TABLE Goft_ForeEver_cus
ADD CONSTRAINT sys_cus_email_UK UNIQUE (email);
CREATE TABLE Goft_ForeEver_Course_Info(
course_Name VARCHAR2(20),
city VARCHAR2(30) CONSTRAINT sys_courtInf_city_nn NOT NULL,
prov VARCHAR2(20) CONSTRAINT sys_courtInf_prov_nn NOT NULL,
postal_Code VARCHAR2(10) CONSTRAINT sys_courtInf_posC_nn NOT NULL,
star_rating NUMBER(1,1),
discript VARCHAR2(200) CONSTRAINT sys_courtInf_descript_uk UNIQUE,
year_Build DATE CONSTRAINT sys_courtInf_date_nn NOT NULL,
court_length Number(5,2)
CONSTRAINT sys_courtInf_courtL_ck CHECK( court_length = 'YARD')
CONSTRAINT sys_courtInf_courtL_nn NOT NULL,
CONSTRAINT sys_courtInf_pk PRIMARY KEY (course_Name, star_rating )
);
CREATE TABLE Goft_ForeEver_favorite_courses(
course_Name VARCHAR2(20),
customer_Id VARCHAR2(10),
CONSTRAINT sys_favCourt_PK PRIMARY KEY(course_Name,customer_Id),
CONSTRAINT sys_favCourt_FK1 FOREIGN KEY (customer_Id) REFERENCES Goft_ForeEver_cus(customer_Id),
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
);
);
Error
Error report - ORA-02256: number of referencing columns must match referenced columns 02256. 00000 - "number of referencing columns must match referenced columns" *Cause: The number of columns in the foreign-key referencing list is not equal to the number of columns in the referenced list. *Action: Make sure that the referencing columns match the referenced columns.
It is because Goft_ForeEver_Course_Info has primary key as
CONSTRAINT sys_courtInf_pk PRIMARY KEY (course_Name, star_rating )
while Goft_ForeEver_favorite_courses that references it has foreign key as
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
See? You're referencing COURSE_NAME, STAR_RATING composite key by COURSE_NAME only - that won't work.
Therefore, either remove STAR_RATING from the primary key in Goft_ForeEver_Course_Info, add STAR_RATING to Goft_ForeEver_favorite_courses, or redesign everything alltogether.
Illustration (SQL*Plus, which points to the error):
SQL> CREATE TABLE Goft_ForeEver_favorite_courses(
2 course_Name VARCHAR2(20),
3 customer_Id VARCHAR2(10),
4 CONSTRAINT sys_favCourt_PK PRIMARY KEY(course_Name,customer_Id),
5 CONSTRAINT sys_favCourt_FK1 FOREIGN KEY (customer_Id) REFERENCES Goft_ForeEver_cus(customer_Id),
6 CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
7 );
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
*
ERROR at line 6:
ORA-02270: no matching unique or primary key for this column-list
SQL>
The problem here is you have declared the "course_Name" along with "star_rating" column as composite primary key in Goft_ForeEver_Course_Info table whereas you are referencing only one column "course_Name" in sys_favCourt_FK2 foreign key declaration in Goft_ForeEver_favorite_courses table.

Can a unique key ( not a primary key) be a foreign key to other table?

I have two table students and studentsprofilepic
'username' from students is unique key of the table
it is referenced as foreign key for the 'studentsprofilepic' table
the DDL for the tables are
CREATE TABLE students (
id NUMBER,
username VARCHAR2(30),
password VARCHAR2(30),
firstname VARCHAR2(30),
lastname VARCHAR2(40),
email VARCHAR2(300),
dob VARCHAR2(20),
alt_email VARCHAR2(300),
street_address VARCHAR2(50),
address_2 VARCHAR2(50),
city VARCHAR2(30),
state VARCHAR2(30),
zip VARCHAR2(10),
country VARCHAR2(60),
telephone VARCHAR2(10),
CONSTRAINT student_id_pk PRIMARY KEY (id),
CONSTRAINT student_username_uk UNIQUE (username)
);
CREATE TABLE studentsprofilepic (
id NUMBER,
photo_id NUMBER,
photo BLOB,
PRIMARY KEY (photo_id),
FOREIGN KEY (username) REFERENCES students (username)
);
YES, The foreign key column establishes a direct relationship with a primary key or unique key column (referenced key) usually in another table:
CREATE TABLE BOOK(
BNAME VARCHAR2(10)NOT NULL UNIQUE,
BTYPE VARCHAR2(10));
CREATE TABLE BOOKS_AUTH(
A_ID INT NOT NULL,
BNAME_REF VARCHAR2(10) NOT NULL,
FOREIGN KEY (BNAME_REF) REFERENCES BOOK (BNAME));
SQLFIDDLE DEMO
Yes, why not. It is possible to reference a UNIQUE constraint in a FOREIGN KEY.
You could have a Primary key and an Unique key, and you would like to validate both.
Yes, you can reference a column (or columns) governed by either a primary key constraint or a unique constraint.
The problem with your table "studentsprofilepic" is that your foreign key tries to use the column "studentsprofilepic"."username", but that column doesn't exist.
create table studentsprofilepic(
id number,
photo_id number,
photo blob,
-- Add the "username" column.
username varchar2(30) not null,
primary key (photo_id),
foreign key (username) references students (username)
);
Also, ask yourself "What's the point of "studentsprofilepic"."id"?" It's not the primary key. It's not a foreign key. It doesn't seem to serve any purpose besides letting you say, "Hey, my table has a column named 'id'!" That's a questionable feature.
Think about adding more not null constraints.

ORA-00911: invalid character

I am trying to write my first table create script with column definitions. I have tried several things to get this error to go away, but to no avail. Please take a look to see if their is anything obvious in my code below:
CREATE TABLE CD_TYPE (
CD_TYPE VARCHAR2(4) PRIMARY KEY,
CD_FORMAT VARCHAR2(10)
);
CREATE TABLE MANUFACTURER (
MANUFACTURER_NUM NUMBER(3) PRIMARY KEY,
MANUFACTURER_NAME VARCHAR2(30)
);
CREATE TABLE CD_TITLE (
CD_NUM NUMBER(4) PRIMARY KEY,
TITLE VARCHAR2(30),
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
CD_TYPE VARCHAR2(4) FOREIGN KEY,
ACQUIRED_DATE DATE,
ORIGINAL CHAR(1)
);
CREATE TABLE CD_SN (
CD_NUM NUMBER(4) PRIMARY KEY FOREIGN KEY,
SERIAL_NUM VARCHAR2(30) PRIMARY KEY,
NUM_LIC_REMAIN NUMBER(2)
);
The problem is with the columns reading
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
You forgot to specify what table these columns refer to.
You to that with a REFERENCES clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num REFERENCES MANUFACTURER,
cd_type REFERENCES CD_TYPE,
acquired_date date,
original char(1)
);
Alternatively, you can specify the name for the foreign key with a CONSTRAINT ... FOREIGN KEY (...) REFERENCES ... clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num,
cd_type,
acquired_date date,
original char(1),
--
CONSTRAINT cd_title_fk1 FOREIGN KEY (manufacturer_num) REFERENCES manufacturer,
CONSTRAINT cd_title_fk2 FOREIGN KEY (cd_type ) REFERENCES cd_type
)

missing right parenthesis problems

i didn't know what the problem is. First this one works fine in sql
create table Department
(Department_Id number(8) PRIMARY KEY ,
Dept_Name varchar(20),
Location varchar(20));
but the second one says missing right parenthesis ora-00907
create table Instructor(Instructor_ID number(8) PRIMARY KEY ,
Department_Id number(8) FOREIGN KEY REFERENCES Department(Department_Id) ,
Ins_name varchar2(20) ,
Position varchar(20) ,
email_Id varchar (40),
Contact_No number(10),
Date_Of_Joining date);
Leave out the FOREIGN KEY part:
Department_Id number(8) REFERENCES Department(Department_Id) ,
See Oracle FAQs for an example. The FOREIGN KEY keywords are for out-of-line constraints, which go after the column definitions.

Can two or more tables which share same foreign keys share a constraint on that foreign key?

What is the problem with this code??
It gives error "name already used by another constraint". Also if I can't define same constraint in different tables then is there any way I can reuse the previously defined constraint?
Any insight??
CREATE TABLE tbl_formats
(
format_id NUMBER(5),
format_name VARCHAR2(50),
format_desc VARCHAR2(100),
valid_from DATE,
valid_to DATE,
format_type VARCHAR2(50),
CONSTRAINT pk_format_id PRIMARY KEY(format_id)
);
CREATE TABLE tbl_format_detail
(
id NUMBER(10),
format_id NUMBER(5),
src_field VARCHAR2(200),
target_field VARCHAR2(100),
business_rule VARCHAR2(4000),
expression VARCHAR2(4000),
target_segment VARCHAR2(4),
CONSTRAINT pk_id PRIMARY KEY(id),
CONSTRAINT fk_format_id FOREIGN KEY(format_id) REFERENCES tbl_formats(format_id)
);
CREATE TABLE tbl_client_formats
(
client_format_id NUMBER(10),
format_id NUMBER(5),
client_id NUMBER(5),
CONSTRAINT pk_client_format_id PRIMARY KEY(client_format_id),
CONSTRAINT fk_format_id FOREIGN KEY(format_id) REFERENCES tbl_formats(format_id),
CONSTRAINT fk_client_id FOREIGN KEY(client_id) REFERENCES tbl_clients(client_id)
);
It seem like the foreign key constraint 'fk_format_id' defined in the table 'tbl_client_formats' conflicts with the same constraint already defined in the table 'tbl_format_detail'.
I am new to oracle so explain even the obvious things please.
The problem is that you're trying to use the same constraint name twice.
Just use a different name for your second constraint (e.g. fk_client_formats_format_id), and you should be fine.
Generally, I'd recommend using the table name as part of the constraint name, to avoid name clashes (if the constraint name gets too long, you'll have to use some kind of abbreviation scheme).
Foreign key are stored in a database range, not a table range. You cannot have two FK with the same name on the same database, even if they are not in the same table. You could name your FK that way:
FK_PARENT_CHILD_FIELD
ex:
FK_FORMATDETAILS_FORMATS_ID,
FK_CLIENTFORMATS_FORMATS_ID,
FK_CLIENTFORMATS_CLIENT_ID

Resources