missing right parenthesis SQL - oracle

I am new to SQL.
I am trying to create a table:
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) FOREIGN KEY REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) FOREIGN KEY REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
However it comes up with: ORA-00907: missing right parenthesis
I know for a fact that you can have to foreign keys, so that's not the problem. Could someone give me a had with the solution of creating the table?

A foreign key actually has to refer to something. In your case you have to tell Oracle what AccountType and CustomerRef refer to. Usually it looks as follows:
AccountType VARCHAR(1) FOREIGN KEY REFERENCES TABLE_NAME(COLUMN_NAME)
Of course, you have to replace TABLE_NAME and COLUMN_NAME with the name of the table and the name of the column you're referring to.

There are two notations you can use when declaring constraints:
1) inline (a constraint):
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
2) out-of-line
CREATE TABLE account
(
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
FOREIGN KEY(AccountType) REFERENCES account_type(TypeCode),
FOREIGN KEY(CustomerRef) REFERENCES account_type(TypeCode)
);
In both cases you can prepend the constraint declaration with CONSTRAINT <name> to give your name to a constraint, otherwise Oracle assigns its own name.
inline notation is applied to a column where the constraint is declared, out-of-line is applied to the table. There slight differences in syntax + some restrictions, for example you cannot declare out-of-line NULL constraint.
You can use inline and out-of-line syntax in CREATE TABLE and ALTER TABLE. Refer to Oracle documentation for more information

You need to define the foreign keys with REFERENCES. In the below snippet, replace AccountTypeTable and CustomerRefTable with the correct table names and replace typeColumn and refColumn with the correct column names in those tables these match to.
UPDATE
I added in the correct values from your comment on another answer
CREATE TABLE account (
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
CONSTRAINT account_fk1 FOREIGN KEY (AccountType) REFERENCES Account_Type(TypeCode),
CONSTRAINT account_fk2 FOREIGN KEY (CustomerRef) REFERENCES Bank_Customer(CustomerRef));

you are missing foreign key syntax...
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY (P_ID),
AccountType VARCHAR(1) FOREIGN KEY (F1_ID) REFERENCES <table_name>(field_name>),
CustomerRef NUMBER(2) FOREIGN KEY (F2_ID) REFERENCES <table_name>(field_name>),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));

Related

I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value , it says that I have 4 columns

For the Epms table, I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value, it says that I have 4 columns. The extra one for City_Id. All I want is to avoid having City_Id column in this table. I declare it because I need it just as FK.
CREATE TABLE City (
City_Id char(3),
state varchar(30),
Primary key (City_Id)
);
create table Emps (
Emp_number varchar(30) primary key,
City_Id char(3),
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id)
);
This is pretty straightforward, just specify e.g. , FOREIGN KEY (arrival_city_id) REFERENCES city(city_id) and you'll get an index.
CREATE TABLE emps (
emp_number varchar(30) NOT NULL,
city_id char(3) NOT NULL,
departure_city_id char(3) NOT NULL,
arrival_city_id char(3) NOT NULL,
PRIMARY KEY (emp_number),
KEY city_id (city_id),
KEY departure_city_id (departure_city_id),
KEY arrival_city_id (arrival_city_id),
CONSTRAINT emps_ibfk_1 FOREIGN KEY (city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_2 FOREIGN KEY (departure_city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_3 FOREIGN KEY (arrival_city_id) REFERENCES city (city_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
" I want is to avoid having City_Id column in this table. I declare it because I need it just as FK."
But you don't need it. The foreign key columns are the arrival and departure columns, and those are the ones you must reference in the constraint declarations:
create table Emps (
Emp_number varchar(30) primary key,
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (departure_City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(arrival_City_Id)
);
Naming constraints is optional but you may find it helpful when debugging foreign key failures, especially when a table has multiple constraints referencing the same parent key.

oracle identifier too long

New to oracle
Im trying to assign composite key
CREATE TABLE rep_contracts
( Store_id NUMBER(8),
Name NUMBER(5)
Quarter CHAR(3),
Rep_id NUMBER(5),
CONSTRAINT rep_contracts_rep_idstore_id_pk PRIMARY KEY (rep_id, store_id),
CONSTRAINT rep_contracts_rep_id_fk FOREIGN KEY (rep_id),
CONSTRAINT rep_contracts_store_id_fk FOREIGN KEY (store_id) );
and im receiving
ORA-00972: identifier is too long
Im aware that composit key identifier longer than 30 char
but if I cut identifier few character shorter ex:
rep_contrac_rep_idstore_id_pk
then I receive
ORA-00905: missing keyword
I couldn't find any workaround
I cleaned the script and incorporated the comments:
added a comma after number(5)
changed all names to uppercase to save pain later
changed NAME which is an Oracle reserved key work to S_NAME
changed the data type from CHAR to VARCHAR2
added the references keyword to point to the table where the foreign key is
renamed the constraint so it is not more than 30 characters
like so:
CREATE TABLE rep_contracts
( STORE_ID NUMBER(8),
S_NAME NUMBER(5),
QUARTER VARCHAR2(3),
REP_ID NUMBER(5),
CONSTRAINT rep_cont_rep_idstore_id_pk PRIMARY KEY (REP_ID, STORE_ID),
CONSTRAINT rep_contracts_rep_id_fk FOREIGN KEY REFERENCES your_table(REP_ID),
CONSTRAINT rep_contracts_store_id_fk FOREIGN KEY REFERENCES your_table(STORE_ID) );

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.

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

Oracle foreign key relation

I have a composite primary key in my Candidate table
CREATE TABLE CANDIDATE(
CANDIDATE_ID VARCHAR(5),
NAME VARCHAR(30),
TELEPHONE NUMBER,
PRIMARY KEY(CANDIDATE_ID, NAME));
When I create a child table, I get an error saying the number of referencing columns must match referenced columns when I create a foreign key for the CANDIDATE_ID
CREATE TABLE JOB(
POSITION_ID VARCHAR(5) PRIMARY KEY,
CANDIDATE_ID VARCHAR(5),
DATE2 DATE,
FOREIGN KEY(CANDIDATE_ID) REFERENCES CANDIDATE);
A table can only have one primary key-- you have a composite primary key. If you have a composite primary key, you have to reference the entire key in your child table. That would mean that the child table would need to have a CANDIDATE_ID column and a NAME column.
CREATE TABLE job (
position_id VARCHAR2(5) PRIMARY KEY,
candidate_id VARCHAR2(5),
name VARCHAR2(30),
date2 DATE,
FOREIGN KEY( candidate_id, name ) REFERENCES candidate( candidate_id, name )
);
Of course, you probably don't want to store the name in both tables. You probably want the candidate_id to be the prmiary key of candidate and you may want to create a separate unique constraint on name.
CREATE TABLE CANDIDATE(
CANDIDATE_ID VARCHAR(5) primary key,
NAME VARCHAR(30) unique,
TELEPHONE NUMBER);
CREATE TABLE JOB(
POSITION_ID VARCHAR(5) PRIMARY KEY,
CANDIDATE_ID VARCHAR(5),
DATE2 DATE,
FOREIGN KEY(CANDIDATE_ID) REFERENCES CANDIDATE(candidate_id));
Assuming that the combination of CANDIDATE_ID and NAME is required for the key to be unique, then you will need to add a reference to the NAME column in your referencing table.
I suspect that CANDIDATE_ID is enough to uniquely identify the candidates in your primary table. If that is the case then it should be your primary key and your relationship will work. If you want to index the NAME separately then do so, but leave it out of the primary key.
Last line should be like this;
CONSTRAINT FK_CANDIDATE_ID FOREIGN KEY (CANDIDATE_ID)REFERENCES CANDIDATE(CANDIDATE_ID);
CREATE TABLE dept
( did char(3) not null,
dname varchar2(20) not null,
CONSTRAINT dept_pk PRIMARY KEY (did)
);
strong text
create table emp
(
eid char(3) unique,
ename varchar2(10) not null,
sal number check (sal between 20000 AND 50000),
city varchar2(10) default 'texus',
did char(3) not null,
constraint fk_did_dept
FOREIGN KEY (did) references
dept(did)
);

Resources