create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000),
date_logged date
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
The table works when i create it without date element. But when i add the date element to it says:
ORA-02253: constraint specification not allowed here
The table works when i create it without date element
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000), -- comma
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
You have to add , before constraint:
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000),
date_logged date, -- here
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
I would also reconsider datatype of log_id/voter_id as (NUMBER/INTEGER).
Try this:
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000) CONSTRAINT abc REFERENCES voters(voter_ID)
)
Related
I want to create a table department:
COLUMN NAME DATATYPE SIZE CONSTRAINT
dept_id number 4 Primary key
prod_id number 4 Foreign key
I tried this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id number(4) foreign key);
It shows error. How can I add a foreign key constraint to this table?
A foreign key defines a relationship between your table DEPARTMENT and another table with a primary key. It means, you cannot create a row in DEPARTMENT with a PROD_ID of 1234 unless there is a pre-existing row in the designated parent table with a value of 1234 as its primary key.
So do you have such an existing parent table? if so you need to include its name in the foreign key definition. Otherwise you must create it.
Let's say the parent table is PRODUCT:
create table product (
prod_id number(4) primary key
, name varchar2(32) not null
);
Then you can create DEPARTMENT with a foreign key like this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id references PRODUCT );
Yep, that's all the syntax you need: it automatically creates a column PROD_ID with the same datatype and precision as the primary key column of the referenced table. More verbose syntax is available. Read the Oracle SQL documentation to find out more.
I assume that the other table is named other_table_name and that it contains a primary key named prod_id.
CREATE Department (
dept_id number(4) primary key,
prod_id number(4) REFERENCES other_table_name (prod_id)
);
or a different syntax
CREATE Department (
dept_id number(4) primary key,
prod_id number(4)
...
CONSTRAINT fk_prod_id
FOREIGN KEY (prod_id)
REFERENCES other_table_name (prod_id)
);
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
)
im working with oracle right now, but im getting a problem when a load this .sql file
CREATE TABLE TIENDA(
ID_TIEND NUMBER,
NOMB_TIEND VARCHAR2(40),
RIF VARCHAR2 (15),
TELF VARCHAR2 (15),
FAX VARCHAR2 (15),
CAPACIDAD_PROD NUMBER,
ID_CIUD NUMBER,
CONSTRAINT ID_TIEND_PK PRIMARY KEY (ID_TIEND),
);
CREATE TABLE CATEGORIA(
ID_CAT NUMBER,
NOMB_CAT VARCHAR2(20),
CONSTRAINT ID_CAT_PK PRIMARY KEY (ID_CAT)
);
CREATE TABLE SUBCATEGORIA(
ID_SUB NUMBER,
NOMB_SUB VARCHAR2(20),
ID_CAT NUMBER,
CONSTRAINT ID_SUB_PK PRIMARY KEY (ID_SUB),
);
CREATE TABLE MARCA(
ID_MARCA NUMBER,
NOMB_MARCA VARCHAR2 (20),
CONSTRAINT ID_MARCA_PK PRIMARY KEY (ID_MARCA)
);
CREATE TABLE PROVEEDOR(
ID_PROV NUMBER,
NOMBRE VARCHAR2(30),
RIF VARCHAR2(15),
TELF VARCHAR2(15),
ID_CIUD NUMBER,
CONSTRAINT ID_PROV_PK PRIMARY KEY (ID_PROV),
);
CREATE TABLE ESTADO(
ID_EST NUMBER,
NOMB_EST VARCHAR2(20),
SIGLAS VARCHAR2 (2),
CONSTRAINT ID_EST_PK PRIMARY KEY (ID_EST)
);
CREATE TABLE CIUDAD(
ID_CIUD NUMBER,
NOMB_CIUD VARCHAR2(20),
SIGLAS VARCHAR2(2),
ID_EST NUMBER,
CONSTRAINT ID_CIUD_PK PRIMARY KEY (ID_CIUD),
);
CREATE TABLE PROVEE(
FECHA_REC DATE,
FECHA_ENV DATE,
CANT NUMBER,
ID_PROV NUMBER,
ID_ALM NUMBER,
ID_PROD NUMBER,
COSTO_PROD FLOAT,
COSTO_ENV FLOAT,
COSTO_TOTAL FLOAT,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA_REC,FECHA_ENV),
);
CREATE TABLE ABASTECE (
FECHA_REC DATE,
FECHA_DESC DATE,
ID_ALM NUMBER,
ID_TIEND NUMBER,
ID_PROD NUMBER,
CANT NUMBER,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA_REC,FECHA_DESC),
);
CREATE TABLE PRODUCTO(
ID_PROD NUMBER,
NOMBRE_PROD VARCHAR2(30),
ID_MARCA NUMBER,
ID_SUB NUMBER,
PVP FLOAT,
CONSTRAINT ID_PROD_PK PRIMARY KEY (ID_PROD),
);
CREATE TABLE TIENDA(
ID_TIEND NUMBER,
NOMB_TIEND VARCHAR2(40),
RIF VARCHAR2 (15),
TELF VARCHAR2 (15),
FAX VARCHAR2 (15),
CAPACIDAD_PROD NUMBER,
ID_CIUD NUMBER,
CONSTRAINT ID_TIEND_PK PRIMARY KEY (ID_TIEND),
);
CREATE TABLE ALMACEN(
ID_ALM NUMBER,
NOMB_ALM VARCHAR2(40),
RIF VARCHAR2(15),
TELF VARCHAR2(15),
DIMENSIONES VARCHAR2(15),
CAPACIDAD_PROD NUMBER,
ID_CIUD NUMBER,
CONSTRAINT ID_ALM_PK PRIMARY KEY (ID_ALM),
);
CREATE TABLE TIENE(
FECHA DATE,
ID_TIEND NUMBER,
ID_PROD NUMBER
CANT_VEND NUMBER,
CANT_EXIST NUMBER,
NOPA NUMBER,
NMRD NUMBER,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA),
);
CREATE TABLE ALMACENA(
FECHA DATE,
ID_ALM NUMBER,
ID_PROD NUMBER,
CANT_DESP NUMBER,
CANT_EXIST NUMBER,
NOPAL NUMBER,
NMRS NUMBER,
CONSTRAINT FECHA_PK PRIMARY KEY (FECHA),
);
ALTER TABLE SUBCATEGORIA
ADD CONSTRAINT ID_SUB_FK
FOREIGN KEY (ID_CAT)
REFERENCES CATEGORIA(ID_CAT);
ALTER TABLE CIUDAD
ADD CONSTRAINT ID_EST_FK
FOREIGN KEY (ID_EST)
REFERENCES ESTADO(ID_EST);
ALTER TABLE PROVEE
ADD CONSTRAINT ID_PROV_FK
FOREIGN KEY (ID_PROV)
REFERENCES PROVEEDOR(ID_PROV);
ALTER TABLE PROVEE
ADD CONSTRAINT ID_ALM_FK
FOREIGN KEY (ID_ALM)
REFERENCES ALMACEN(ID_ALM);
ALTER TABLE PROVEE
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
ALTER TABLE ABASTECE
ADD CONSTRAINT ID_TIEND_FK
FOREIGN KEY (ID_TIEND)
REFERENCES TIENDA(ID_TIEND);
ALTER TABLE ABASTECE
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
ALTER TABLE ABASTECE
ADD CONSTRAINT ID_ALM_FK
FOREIGN KEY (ID_ALM)
REFERENCES ALMACEN(ID_ALM);
ALTER TABLE PRODUCTO
ADD CONSTRAINT ID_SUBC_FK
FOREIGN KEY (ID_SUB)
REFERENCES SUBCATEGORIA(ID_SUB);
ALTER TABLE TIENDA
ADD CONSTRAINT ID_CIUD_FK
FOREIGN KEY (ID_CIUD)
REFERENCES CIUDAD(ID_CIUD);
ALTER TABLE ALMACEN
ADD CONSTRAINT ID_CIUD_FK
FOREIGN KEY (ID_CIUD)
REFERENCES CIUDAD(ID_CIUD);
ALTER TABLE PROVEEDOR
ADD CONSTRAINT ID_CIUD_FK
FOREIGN KEY (ID_CIUD)
REFERENCES CIUDAD(ID_CIUD);
ALTER TABLE TIENE
ADD CONSTRAINT ID_TIEND_FK
FOREIGN KEY (ID_TIEND)
REFERENCES TIENDA(ID_TIEND);
ALTER TABLE TIENE
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
ALTER TABLE ALMACENA
ADD CONSTRAINT ID_ALM_FK
FOREIGN KEY (ID_ALM)
REFERENCES ALMACEN(ID_ALM);
ALTER TABLE ALMACENA
ADD CONSTRAINT ID_PROD_FK
FOREIGN KEY (ID_PROD)
REFERENCES PRODUCTO(ID_PROD);
When i load it in the terminal with this command "SQL> start D:\lab2.sql" this error comes out
A little help? thanks.
In some of your commands - e.g. the first one - you seem to have removed a line before the ending parenthesis. But you have not at the same time removed the trailing comma from the preceding line.
I cant say if this will solve all your errors - but certainly some:-)
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.
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)
);