Here is the DDL command:
CREATE TABLE STUDENT
(
student_id int(11) CONSTRAINT st_id_pk PRIMARY KEY,
name varchar2(50) CONSTRAINT st_na_uq NOT NULL UNIQUE,
address varchar2(100) CONSTRAINT st_ad_de DEFAULT 'cornelia',
class_id int(11) CONSTRAINT st_cl_fk REFERENCES CLASS(student_id),
section varchar2(50) NOT NULL,
age int(11) DEFAULT '16',
CHECK (age >= 15)
);
Two issues:
int should not be followed by a precision argument. Either do int or number(11), but not int(11).
DEFAULT is not something that can be specified as a CONSTRAINT. Leave out CONSTRAINT
This will work (provided that the reference to CLASS(student_id) is correct):
CREATE TABLE STUDENT(
student_id int CONSTRAINT st_id_pk PRIMARY KEY,
name varchar2(50) CONSTRAINT st_na_uq NOT NULL UNIQUE,
address varchar2(100) DEFAULT 'cornelia',
class_id int CONSTRAINT st_cl_fk REFERENCES CLASS(student_id),
section varchar2(50) NOT NULL,
age int DEFAULT 16,
CHECK(age>=15)
);
Related
I'm trying to create a "diagnosis" table(last table), but something is wrong with the 2 lines of code that creates Foreign Key. I'm sure that's where the problem lies because when I comment them out, I can create the table. The first 4 tables can be created with no errors, so no problem with them. Thank you for reading my question.
I have tried to spot some syntax errors but to no avail.
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
I expected that the table can be created with no error.
Your diagnosis table refers to out_pat_ID and in_pat_ID as columns which should serve as foreign keys, but these columns don't actually exist in the table definition. Try adding them as one possible fix here:
CREATE TABLE diagnosis (
dia_ref number(12) NOT NULL,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8),
out_pat_ID number(5),
in_pat_ID number(5)
CONSTRAINT dia_pk PRIMARY KEY (dia_ref),
CONSTRAINT dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINT dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
diagnosis table need add out_pat_ID and in_pat_ID column
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
✓
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
✓
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
✓
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
✓
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5) not null,
in_pat_ID number(5) not null,
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
✓
db<>fiddle here
If you are allowing only one patient per diagnosis, then you should have a check constraint as well. Your comment and data structure are out-of-sync:
CREATE TABLE diagnosis (
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5),
in_pat_ID number(5),
constraint chk_diagnosis_pat_id
check ( (out_pat_id is not null and in_pat_id is null) or
(out_pat_id is null and in_pat_id is not null)
),
constraint dia_pk primary key (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
constraint dia_out_fk foreign key (out_pat_ID) references out_patient (out_pat_ID),
constraints dia_in_fk foreign key (in_pat_ID) references in_patient (in_pat_ID)
);
That said, separate tables for in-patient and out-patient do not look correct. Or, at the very least, you need a persons table with information about a person over time. Then you can have separate "appointments" and "registrations" to determine if they make an out-patient appointment or register for in-patient care.
Personally I prefer to define single-column constraints inline as part of the column definition. Then you don't have to repeat the column name, and for FK constraints you can let the column inherit its parent's datatype so it is less error-prone.
create table in_patient
( in_pat_id number(5) constraint in_pat_pk primary key
, in_pat_name varchar2(20)
, in_pat_add varchar2(50)
, in_pat_dob date
, in_pat_history varchar2(100)
, in_mode_payment varchar2(20)
, in_start_date date
, in_end_date date
);
create table out_patient
( out_pat_id number(5) constraint out_pat_pk primary key
, out_pat_name varchar2(20)
, out_pat_add varchar2(50)
, out_pat_dob date
, out_pat_history varchar2(100)
, out_mode_payment varchar(20)
, out_date_of_visit date
);
create table diagnosis
( dia_ref number(12) constraint dia_pk primary key
, dia_type varchar2(20)
, dia_date date
, dia_result varchar2(8)
, dia_out_fk references out_patient (out_pat_id)
, dia_in_fk references in_patient (in_pat_id)
);
If dia_result can only be 'POSITIVE' or 'NEGATIVE' then I'd suggest adding a check constraint to enforce this.
Probably some more columns could be defined as not null. It's a good idea to define every possible constraint you can think of.
Table One:
CREATE TABLE Customer
(CustNo VARCHAR2(8) CONSTRAINT CustNoNotNull NOT NULL,
CustName VARCHAR2(30) CONSTRAINT CustNameNotNull NOT NULL,
Address VARCHAR2(50) CONSTRAINT AddressNotNull NOT NULL,
Internal CHAR(1) CONSTRAINT InternalNotNull NOT NULL,
Contact VARCHAR2(35) CONSTRAINT ContractNotNull NOT NULL,
Phone VARCHAR2(11) CONSTRAINT CPhoneNotNull NOT NULL,
City VARCHAR2(30) CONSTRAINT CityNotNull NOT NULL,
State VARCHAR2(2) CONSTRAINT StateNotNull NOT NULL,
Zip VARCHAR2(10) CONSTRAINT zipNotNull NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CustNo) ) ;
Table Two:
CREATE TABLE Facility
(FacNo VARCHAR2(8) CONSTRAINT FacNoNotNull NOT NULL,
FacName VARCHAR2(30) CONSTRAINT FacNameNotNull NOT NULL,
CONSTRAINT PK_FACILITY PRIMARY KEY (FacNo)
CONSTRAINT Unique_FacName UNIQUE(FacName) );
Table Three:
CREATE TABLE EVENTREQUEST
( EVENTNO VARCHAR2(8) CONSTRAINT EVENTNONOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELDNOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQNOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNONOTNULL NOT NULL ,
FACNO VARCHAR2(8) CONSTRAINT FACNONOTNULL NOT NULL,
DATEAUTH DATE CONSTRAINT DATEAUTHNULL NULL,
STATUS VARCHAR2(10) CONSTRAINT STATUSNOTNULL NOT NULL,
ESTCOST VARCHAR2(25) CONSTRAINT ESTCOSTNOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(10) CONSTRAINT ESTAUDIENCENOTNULL NOT NULL,
BUDNO VARCHAR2(8) CONSTRAINT BUDNONULL NULL,
CONSTRAINT PK_EVENTREQUEST PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTNO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER (CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY (FACNO),
CONSTRAINT CHECK_EVENTREQUEST_STATUS CHECK(STATUS IN('PENDING','DENIED','APPROVED')));
I get this error executing Table Three:
"The specified constraint name has to be unique. *Action: Specify a unique constraint name for the constraint
How can I prevent this error from occurring?
In Oracle, constraints are a type of object, and they have an identifier (a name by which they are distinguished from other objects). All constraints, on all tables within a schema, share the same name space. Which means you can't have two constraints with the same name in the same schema, even if they are on different tables.
Moreover, identifiers by default are case insensitive. On the second table you defined a constraint FacNoNotNull, and on the third table you are trying to define a constraint FACNONOTNULL. Since identifiers are case insensitive, this is the same name - so you get an exception.
A completely wrong approach (which would work, unfortunately - so many people may be inclined to do it even though it's wrong) is to enclose names in double-quotes, which makes them case sensitive. Don't do that!
Rather, one has to ask - why do you need to name your NOT NULL constraints in the first place? Just add the key words NOT NULL after the column definition; it is very hard to see when or where you would need to know the name of each such constraint.
CREATE TABLE student_detailS01
(USN VARCHAR(20) NOT NULL,
STUDENT_NAME VARCHAR2(20) NOT NULL,
BRANCH VARCHAR2(20) NOT NULL,
CONTACT_NO NUMBER(10) NOT NULL,
FATHERS_NAME VARCHAR2(20) NOT NULL,
HOME_ADDRESS VARCHAR(50) NOT NULL,
DOB DATE NOT NULL,
LOCAL_ADDRESS VARCHAR2(50),
EMAIL_ID VARCHAR2(20),
DOJ DATE,
HID NUMBER NOT NULL,
PRIMARY KEY (USN,HID)
);
CREATE TABLE MESS(
S_NO NUMBER NOT NULL,
HID NUMBER REFERENCES STUDENT_DETAILS01(HID),
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
PRIMARY KEY(S_NO)
);
student_details01 table is executed, but mess table gives the following error:
no matching unique or primary key for this column-list.
The ORA-2270 : no matching unique or primary key for this column-list error is quite simple: it happens when the columns we reference in the foreign key do not match a primary key or unique constraint on the parent table. Common reasons for this are
the parent lacks a constraint altogether
the parent table's constraint is a compound key and we haven't
referenced all the columns in the foreign key statement.
To fix your issue try as below:
CREATE TABLE MESS
(
S_NO NUMBER,
S_HID NUMBER,
S_UNO VARCHAR (20), --Added Column
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
CONSTRAINT MESS_pk PRIMARY KEY (S_NO),
CONSTRAINT fk_id_dtls FOREIGN KEY
(S_HID, S_UNO)
REFERENCES student_detailS01 (HID, USN)
);
I don't have so many experience in this..I've got this error when I tried to insert into table.
Here's code:
CREATE TABLE factory
(idfactory INT NOT NULL,
location_id INT NOT NULL,
owner INT NOT NULL,
CONSTRAINT factory_id_pk PRIMARY KEY(idfactory),
CONSTRAINT f_location_id_fk FOREIGN KEY(location_id) REFERENCES location(idLocation),
CONSTRAINT s_owner_id_fk FOREIGN KEY(owner) REFERENCES employees(idEmployee));
CREATE TABLE location
(idLocation INT NOT NULL,
Name VARCHAR(45),
region_id INT NOT NULL,
CONSTRAINT location_id_pk PRIMARY KEY(idLocation),
CONSTRAINT p_location_id_fk FOREIGN KEY(region_id) REFERENCES region(idRegion));
CREATE TABLE employees
(idEmployee INT NOT NULL,
Name VARCHAR(20) NOT NULL,
location_id INT NOT NULL,
email VARCHAR(45),
CONSTRAINT emp_id_pk PRIMARY KEY(idEmployee),
CONSTRAINT emp_loc_fk FOREIGN KEY(location_id) REFERENCES location(IdLocation);
Insert:
INSERT INTO factory(factory_id_sequence.NEXTVAL,43,23);
And i got this error..I can't see what's mistake.
Thanks a lot!
You need to have the VALUES keyword in the insert statement:
INSERT INTO factory VALUES (factory_id_sequence.NEXTVAL,43,23);
I am new to oracle,
I have created two tables using following queries,
CREATE TABLE employee
(
emp_name VARCHAR(20) NOT NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(emp_name)
)
and
CREATE TABLE company
(
comp_name VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(comp_name)
)
Now I am trying to create another table using some foreign keys,
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
Getting ERROR : ORA-00907: missing right parenthesis
I have also tried with
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
constraint wemployee FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
constraint wcompany FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
But getting same error.
Can any one tell me that where I am doing mistake?
I'm no expert in oracle, but are you allowed to specify the (10) in salary int(10) NOT NULL?
1: you should have a table called "test" with two columns, id and testdata. (This is just a dumb quick example, so I won't bother to specify any constraints on id.)
create table test (id number, testdata varchar2(255));
2: Next we'll create a sequence to use for the id numbers in our test table.
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
You could change "start with 1" to any number you want to begin with (e.g. if you already have 213 entries in a table and you want to begin using this for your 214th entry, replace with "start with 214"). The "increment by 1" clause is the default, so you could omit it. You could also replace it with "increment by n" if you want it to skip n-1 numbers between id numbers. The "nomaxvalue" tells it to keep incrementing forever as opposed to resetting at some point.i (I'm sure Oracle has some limitation on how big it can get, but I don't know what that limit is).
3: Now we're ready to create the trigger that will automatically insert the next number from the sequence into the id column.
create trigger test_trigger
before insert on test
for each row beginselect test_seq.nextval into :new.id from dual;
end;
/
There are two different ways to create a table with constraints:
1)
create table department(
deptno number(5) primary key,
deptname varchar2(30),
empno number(5) references emp(empno));
2)
create table department(
deptno number(5),
deptname varchar2(30),
empno number(5),
constraint pkey_deptno primary key(deptno),
constraint fkey_empno foreign key(empno) references Emp(empno));
When creating the index inline with the rest of the table creation statement try dropping the FOREIGN KEY part:
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
emp_name REFERENCES employee(emp_name),
comp_name REFERENCES company(comp_name)
)
See this question for more details:
ORA-00907: missing right parenthesis