I've been trying to figure this out for a while, but I haven't come to a solution, could you please help me? I have four tables like this, I think the problem may be the foreign keys?
I am using SQL+ (Oracle) and SQL Developer.
This is my code:
create table doctor(
doctor_id int constraint nn_name not null,
fname varchar2(30),
lname varchar2(30),
address varchar2(30),
phone_no number(30),
email varchar2(30),
experience_years number(10),
grade number(10),
specialities varchar(30),
availabilty_date default sysdate,
agency_id int,
foreign key references agency (agency_id);
As the error message says: you need one more ).
The create table ( does not get closed at the end. There were also some other bugs I fixed:
create table doctor
( doctor_id int constraint nn_name not null,
fname varchar2(30),
lname varchar2(30),
address varchar2(30),
phone_no number(30),
email varchar2(30),
experience_years number(10),
grade number(10),
specialities varchar(30),
availabilty_date date default sysdate, -- missing data type
agency_id int,
constraint agency_fk foreign key (agency_id) references agent (agency_id) -- invalid foreign constraint
) -- this one
Related
Here's the code that's causing the issue:
DROP TABLE customers;
--1 create tables
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25),
first_name VARCHAR2(25),
home_phone VARCHAR2(12),
address VARCHAR2(100),
city VARCHAR2(30),
state VARCHAR2(2),
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id),
CONSTRAINT not_null_customer_last_name NOT NULL (last_name),
CONSTRAINT not_null_customer_first_name NOT NULL (first_name),
CONSTRAINT not_null_customer_home_phone NOT NULL (home_phone),
CONSTRAINT not_null_customer_address NOT NULL (address),
CONSTRAINT not_null_customer_city NOT NULL (city),
CONSTRAINT not_null_customer_state NOT NULL (state)
);
I've tried formatting the constraints at the column level and that didn't seem to help. I'm an absolute beginner and am doing this for a class so I'm sure it's something simple and silly but I couldn't figure it out for the life of me. After a few hours of frustration I figured I'd see if there was someone out there who could point me in the right direction.
I am using Oracle APEX.
NOT NULL constraints are defined in-line; there is no option to define them out-of-line (apart from using a CHECK constraint).
From the CREATE TABLE documentation:
inline_constraint::=
out_of_line_constraint::=
Therefore, if you want to use a NOT NULL constraint then you need to declare it inline:
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25)
CONSTRAINT not_null_customer_last_name NOT NULL,
first_name VARCHAR2(25)
CONSTRAINT not_null_customer_first_name NOT NULL,
home_phone VARCHAR2(12)
CONSTRAINT not_null_customer_home_phone NOT NULL,
address VARCHAR2(100)
CONSTRAINT not_null_customer_address NOT NULL,
city VARCHAR2(30)
CONSTRAINT not_null_customer_city NOT NULL,
state VARCHAR2(2)
CONSTRAINT not_null_customer_state NOT NULL,
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id)
);
I think the problem is the way you are defining the NOT NULL constraints, they can be CHECK constraints so you should use:
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25),
first_name VARCHAR2(25),
home_phone VARCHAR2(12),
address VARCHAR2(100),
city VARCHAR2(30),
state VARCHAR2(2),
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id),
CONSTRAINT not_null_customer_last_name CHECK (last_name IS NOT NULL),
CONSTRAINT not_null_customer_first_name CHECK (first_name IS NOT NULL),
CONSTRAINT not_null_customer_home_phone CHECK (home_phone IS NOT NULL),
CONSTRAINT not_null_customer_address CHECK (address IS NOT NULL),
CONSTRAINT not_null_customer_city CHECK (city IS NOT NULL),
CONSTRAINT not_null_customer_state CHECK (state IS NOT NULL)
);
Im doing a course on DB, this is one of the first assignments, I'm about to be done, but there is an issue somewhere in my REL_CURSOS_ALUMNOS query which ends up with the ORA-00906 error.
REL_CURSOS_ALUMNOS is a relationship table for "one to many" 1 ' CURSOS(Id_Curso) to many ALUMNOS(NIF_Alumno).
This is my query till now:
CREATE TABLE PROFESORES(
NIF_Profesor VARCHAR2(15) CONSTRAINT Prof_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Titulacion VARCHAR2(500),
Salario FLOAT(10) CONSTRAINT Prof_sal_NN NOT NULL
);
CREATE TABLE ALUMNOS(
NIF_Alumno VARCHAR2(15) CONSTRAINT Alum_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Sexo CHAR(1),
Fecha_Nacimiento DATE
);
CREATE TABLE CURSOS (
Id_Curso VARCHAR2(15) CONSTRAINT Curs_Id_PK PRIMARY KEY,
Nombre VARCHAR2(400) UNIQUE,
NIF_Profesor VARCHAR2(15) REFERENCES PROFESORES (NIF_Profesor),
Max_Alumnos NUMBER(5),
Inicio_Fecha DATE,
Final_Fecha DATE,
Num_Horas NUMBER(10) NOT NULL,
CONSTRAINT Curs_FechasIncorrectas CHECK (Final_Fecha > Inicio_Fecha));
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Cursos),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE KEY (Id_Curso, NIF_Alumno));
Thank you.
You have a couple of small errors in the final table definition:
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Curso),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE (Id_Curso, NIF_Alumno)
);
Your specific problem is caused by KEY. That is not needed for a unique constraint definition. Further, the reference for CURSOS is id_curso, not id_cursos.
I left both unique definitions. But if NIF_Alumno is unique, then the pair (Id_Curso, NIF_Alumno) is also unique, so that constraint is redundant.
here is my code
Create table Member(
Member_Id Number(5) primary key,
Member_Name varchar2(30),
Member_address varchar2(50),
Acc_Open_Date Date,
Membership_type varchar2(20),
Fees_paid Number(4),
Max_Book_Allowed Number(2),
Penalty_Amount Number(7,2),
CONSTRAINT mt CHECK (Membership_type IN(‘Lifetime’,’Annual’,’Half Yearly’,’Quarterly’))
);
now i want to set the check constraint with some specific string and i use for comparing the IN but it shows again and again invalid character.
now by using alter table it works..
but my query is how can i use in create table statement.
Too long for a comment.
This works for me.
Create table Member(
Member_Id Number(5) primary key,
Member_Name varchar2(30),
Member_address varchar2(50),
Acc_Open_Date Date,
Membership_type varchar2(20),
Fees_paid Number(4),
Max_Book_Allowed Number(2),
Penalty_Amount Number(7,2),
CONSTRAINT mt CHECK (Membership_type IN('Lifetime','Annual','Half Yearly','Quarterly'))
)
Yeah, not too sure on this one. New to sql and I thought everything was done right, any ideas? If this is not the way to add foreign keys, could someone please explain to me how the correct way to do it is please? Would appreciate it, thanks.
CREATE TABLE customer (
reference NUMBER(5) PRIMARY KEY,
company_name VARCHAR2(30),
address VARCHAR2(30),
post_code VARCHAR2(10),
telephone VARCHAR(20),
contact_fname VARCHAR2(20),
contact_sname VARCHAR2(20),
contact_email VARCHAR2(30)
);
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1),
weight NUMBER(10)
);
CREATE TABLE category (
category NUMBER(1) PRIMARY KEY,
description VARCHAR2(15),
requirements VARCHAR2(30),
FOREIGN KEY (category) REFERENCES manifest(category)
);
CREATE TABLE trip (
trip_id NUMBER(10) PRIMARY KEY,
departure_date DATE,
return_date DATE,
vehicle_id VARCHAR2(10),
employee_no NUMBER(10),
FOREIGN KEY (trip_id) REFERENCES manifest(trip_id)
);
CREATE TABLE vehicle (
registration VARCHAR2(10) PRIMARY KEY,
vehicle_type_id VARCHAR2(10),
model VARCHAR2(15),
make VARCHAR2(15),
body VARCHAR2(15),
year NUMBER(4),
FOREIGN KEY (registration) REFERENCES trip(registration)
);
CREATE TABLE model (
vehicle_type_id VARCHAR(10) PRIMARY KEY,
make VARCHAR2(15),
model VARCHAR2(15),
FOREIGN KEY (vehicle_type_id) REFERENCES vehicle(vehicle_type_id)
);
CREATE TABLE driver (
employee_no NUMBER(10) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR(20),
ni_no VARCHAR2(15),
telephone VARCHAR2(20),
mobile VARCHAR2(12),
hazardous_goods VARCHAR2(1),
FOREIGN KEY (employee_no) REFERENCES trip(employee_no)
);
and the error message I get is
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I get this error for every table after manifest btw
The error message is pretty clear. The reference for a foreign key needs to be a unique or primary key in the other table. So this is wrong in the category table.
FOREIGN KEY (category) REFERENCES manifest(category)
I assume you intend for the manifest to look like this:
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10) REFERENCES trip(trip_id),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1) REFERENCES category(category),
weight NUMBER(10)
);
And so on for the other places where foreign keys are used. (This uses a short-hand notation for the foreign key reference; a separate clause in CREATE TABLE is also fine.)
Of course, the definition of manifest has to go after category and trip, in this example.
In other words, the reference goes in the other table, not where the primary key is defined.
Also, I would recommend being consistent with the names of the foreign keys. At the very least, they should generally have the same name as the primary key, where possible.
trip_id, vehicle_type_id, and employee_no all need to be marked as UNIQUE.
create table prmr(
emp_id number(10) primary key,
name varchar2(10),
mob number(10) unique key,
id varchar2(10),
email varchar2(20)
);
When I create this table, it gives me the error "Missing Parenthesis". Any help?
Your syntax is wrong. Remove key from unique key
CREATE TABLE prmr(
emp_id number(10) PRIMARY KEY,
name varchar2(10),
mob number(10) UNIQUE,
id varchar2(10),
email varchar2(20)
);
There is also a way to specify constraints separately:
CREATE TABLE prmr(
emp_id number(10) NOT NULL,
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT mob_unique UNIQUE (mob),
CONSTRAINT emp_id_pk PRIMARY KEY (emp_id)
);
I also added NOT NULL to the emp_id (Although you don't need to specify it explicitly as far as emp_id is PK, that could, probably, help avoid the confusion anyway).
Try this :
create table prmr(
emp_id number(10),
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT PK_table PRIMARY KEY (emp_id),
CONSTRAINT unique_mob UNIQUE(mob)
);