I'm new to SQL and I'm stuck with creating the table
CREATE TABLE Recording(
recordingID int not null,
title varchar(255) not null,
nameRec varchar(255) not null,
price DOUBLE not null,
quantityStock int not null,
musicID int not null,
StockDiscNum int not null,
orderNums int not null,
FOREIGN KEY(orderNums) REFERENCES Orders (orderNumber),
PRIMARY KEY(recordingID)
);
I checked every type but not sure why it say missing keyword
Thank you
What is the exact issue and the exact way to create table in SQL
There's no DOUBLE datatype in Oracle; use NUMBER instead. Also, Oracle recommends us to use varchar2 instead of varchar.
SQL> CREATE TABLE Recording
2 (
3 recordingID INT NOT NULL,
4 title VARCHAR2 (255) NOT NULL,
5 nameRec VARCHAR2 (255) NOT NULL,
6 price NUMBER NOT NULL,
7 quantityStock INT NOT NULL,
8 musicID INT NOT NULL,
9 StockDiscNum INT NOT NULL,
10 orderNums INT NOT NULL,
11 FOREIGN KEY (orderNums) REFERENCES Orders (orderNumber),
12 PRIMARY KEY (recordingID)
13 );
Table created.
SQL>
CREATE TABLE residents
(
R_ID NUMBER(4), CONSTRAINTS pk_residents_R_ID PRIMARY KEY,
R_FN VARCHAR2(15), NOT NULL,
R_LN VARCHAR2(15), NOT NULL,
R_Contact NUMBER(10), NOT NULL,
DoB DATE, NOT NULL
);
Tried a few changes but I'm unable to resolve this error. Any help would be appreciated!
It works for Oracle in this way:
CREATE TABLE residents(
R_ID NUMBER(4),
R_FN VARCHAR2(15) NOT NULL,
R_LN VARCHAR2(15) NOT NULL,
R_Contact NUMBER(10) NOT NULL,
DoB DATE NOT NULL,
pk_residents_R_ID NUMBER(4) PRIMARY KEY
);
Code which is the closest to your attempt is:
SQL> CREATE TABLE residents
2 (
3 r_id NUMBER (4) CONSTRAINT pk_residents_r_id PRIMARY KEY,
4 r_fn VARCHAR2 (15) NOT NULL,
5 r_ln VARCHAR2 (15) NOT NULL,
6 r_contact NUMBER (10) NOT NULL,
7 dob DATE NOT NULL
8 );
Table created.
SQL>
keyword is constraint, not constraintS
don't separate NOT NULL (or any other constraint) from its column with a comma
Also, using mixed letter case is irrelevant as Oracle - by default - stores object names in UPPERCASE. That's so unless you decide to enclose names into double quotes, but then you'll always have to use double quotes and exactly match letter case so - that's not what anyone should do, not in Oracle.
This question already has answers here:
Missing right Paranthesis on Create Table command SQL
(2 answers)
Closed 3 years ago.
I'm getting an error when creating two tables that I'm missing a right paren on the second table although I am not.
I've tried different oracle variations of this code and I'm still getting the error.
(ORACLE LIVE SQL)
CREATE TABLE PET_OWNER
(OwnerID Integer Primary Key,
OwnerLastName Char(25) Not Null,
OwnerFirstName Char(25) Not Null,
OwnerPhone Char(25) Null,
OwnerEmail Char(50) Not Null);
CREATE TABLE PET_DATA
(PetID Integer Not Null,
PetName Char(50) Not Null,
PetType Char(25) Not Null,
PetBreed Char(50) Not Null,
PetDOB Varchar(50) Not Null,
Primary Key (PetID)
Constraint FK_PetOwner Foreign Key (OwnerID)
References Owner(OwnerID));
I expect the tables to be created but only the first table is being created successfully. The second table has a foreign key.
It looks like you are missing a comma after the primary-key definition on the second table.
The Oracle parser often complains about missing closing parentheses when the real issue is some other syntax error.
I would recommend:
CREATE TABLE PET_OWNER (
OwnerID Integer Primary Key,
OwnerLastName varchar2(25) Not Null,
OwnerFirstName varchar2(25) Not Null,
OwnerPhone varchar2(25) Null,
OwnerEmail varchar2(50) Not Null
);
CREATE TABLE PET_DATA (
PetID Integer Not Null,
OwnerID Integer,
PetName varchar2(50) Not Null,
PetType varchar2(25) Not Null,
PetBreed varchar2(50) Not Null,
PetDOB varchar2(50) Not Null,
Primary Key (PetID),
Constraint FK_PetOwner Foreign Key (OwnerID) References Pet_Owner(OwnerID)
);
This fixes small problems (missing OwnerId column in the second table, wrong table name). It also uses varchar2() for variable length strings rather than char() -- which are padded with spaces to the specified length.
This question already has answers here:
SQL Error: ORA-02291: integrity constraint
(4 answers)
Closed 7 years ago.
We are trying to insert data to our tables, however we have run into an error and can't see the problem. This is what we are getting -
INSERT INTO Item(Manifest_barcode,Trip_ID,Item_weight,Pickup_customer,Delivery_customer,Category) VALUES (159601450,73495,2156,166,184,'A')
Error report -
SQL Error: ORA-02291: integrity constraint (HR.SYS_C009055) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
We have checked the order of the creation of the tables, and as far as we can tell everything is in the right place. All of the foreign keys seem to be correct as well.
This is how we are dropping/creating the tables -
DROP TABLE Item;
DROP TABLE Trip;
DROP TABLE Vehicle;
DROP TABLE Vehicle_Type;
DROP TABLE Employee;
DROP TABLE Customer;
DROP TABLE Category;
CREATE TABLE Category(
Category VARCHAR2(100) NOT NULL,
Description VARCHAR2(100) NOT NULL,
Requirements VARCHAR2(100),
PRIMARY KEY(Category)
);
CREATE TABLE Customer(
Reference INT NOT NULL,
Name VARCHAR2(100) NOT NULL,
Address VARCHAR2(100) NOT NULL,
Town VARCHAR2(100) NOT NULL,
Post_code VARCHAR2(8) NOT NULL,
Telephone INT NOT NULL,
Contact_first_name VARCHAR2(100) NOT NULL,
Contact_last_name VARCHAR2(100) NOT NULL,
Email VARCHAR2(100) NOT NULL,
PRIMARY KEY(Reference)
);
CREATE TABLE Employee(
Employee_no INT NOT NULL,
First_name VARCHAR2(100) NOT NULL,
Last_name VARCHAR2(100) NOT NULL,
NI_No VARCHAR2(100) NOT NULL,
Telephone VARCHAR2(100) NOT NULL,
Mobile VARCHAR2(100) NOT NULL,
Hazardous_goods VARCHAR2(100) NOT NULL,
PRIMARY KEY(Employee_no)
);
CREATE TABLE Vehicle_Type(
Vehicle_Type_ID VARCHAR2(100) NOT NULL,
Model VARCHAR2(100) NOT NULL,
Make VARCHAR2(100) NOT NULL,
PRIMARY KEY(Vehicle_Type_ID)
);
CREATE TABLE Vehicle(
Registration VARCHAR2(100) NOT NULL,
Vehicle_Type_ID VARCHAR2(100) NOT NULL,
GVW VARCHAR2(100) NOT NULL,
Vehicle_Year INT NOT NULL,
Body VARCHAR2(100),
PRIMARY KEY(Registration),
FOREIGN KEY(Vehicle_Type_ID) REFERENCES Vehicle_Type(Vehicle_Type_ID)
);
CREATE TABLE Trip(
Trip_ID INT NOT NULL,
Departure_date VARCHAR2(100) NOT NULL,
Return_date VARCHAR2(100) NOT NULL,
Employee_no INT NOT NULL,
Vehicle_registration VARCHAR2(100) NOT NULL,
PRIMARY KEY(Trip_ID),
FOREIGN KEY(Employee_no) REFERENCES Employee(Employee_no)
);
CREATE TABLE Item(
Manifest_barcode VARCHAR2(100) NOT NULL,
Trip_ID INT NOT NULL,
Item_weight INT NOT NULL,
Pickup_customer INT NOT NULL,
Delivery_customer INT NOT NULL,
Category VARCHAR2(100) NOT NULL,
PRIMARY KEY(Manifest_barcode),
FOREIGN KEY(Trip_ID) REFERENCES Trip(Trip_ID),
FOREIGN KEY(Category) REFERENCES Category(Category)
);
This is how the items are being inserted -
INSERT INTO Customer (Name,Reference,Address,Town,Post_code,Telephone,Contact_first_name,Contact_last_name,Email) VALUES
('Calash Ltd.',1,'88 Rinkomania Lane','Cardigan','SA55 8BA',11671595763,'Cameron','Dunnico','c.dunnico#calash.co.uk');
INSERT INTO Employee (Employee_no,First_name,Last_name,NI_No,Telephone,Mobile,Hazardous_goods) VALUES
(0045619,'Eamon','O''Looney','JJ 56 53 26 B','1656727840','76599770175','N');
INSERT INTO Vehicle_Type (Vehicle_Type_ID,Model,Make) VALUES
(1,'RIEVER','ALBION');
INSERT INTO Vehicle(Registration,Vehicle_Type_ID,GVW,Vehicle_Year,Body) VALUES
('4585 AW',1,20321,1963,'');
INSERT INTO Category (Category, Description, Requirements) VALUES
('A','Normal','');
INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration) VALUES
(72943,'40910','40914',0028539,'BR58BXE');
INSERT INTO Item(Manifest_barcode,Trip_ID,Item_weight,Pickup_customer,Delivery_customer,Category) VALUES
(541769754,73421,3629,44,145,'A');
Anyone have any suggestions?
Your full insert script has 13 inserts into the item table with trip_id 73495. Your error is being thrown from the first one:
INSERT INTO Item(Manifest_barcode,Trip_ID,Item_weight,Pickup_customer,Delivery_customer,Category)
VALUES (159601450,73495,2156,166,184,'A');
Your script does not have a matching insert for the trip table. You have IDs either side:
INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration)
VALUES (73494,'40994','40995',0077517,'PY11 OAA');
INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration)
VALUES (73496,'40994','41000',0083413,'PY58 UHF');
But there is not one for ID 73495.
Searching that script for 73495 only matches those 13 item inserts, and a later item which has a manifest_barcode of 617349505 which contains it. But that is all.
There is no matching trip, which is what the exception is telling you.
Hi please check the following
you have inserted this row in the employee table with employee_no=0045619
INSERT INTO Employee (Employee_no,First_name,Last_name,NI_No,Telephone,Mobile,Hazardous_goods) VALUES
(0045619,'Eamon','O''Looney','JJ 56 53 26 B','1656727840','76599770175','N');
trip is the table which was referencing the employee table
INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration) VALUES
(72943,'40910','40914',0028539,'BR58BXE');
in this insert statement you had inserted a row with employee_no=0028539, which was not matched with the employee_no(0045619) available in the employee table.
please correct the value and try to insert with the same employee number in the employee table
---------editied----------------
check the tripid in item(73421) is available in the trip table?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I created the following table definition in netbeans ide 8.
CREATE TABLE DOCTOR_INFO(
DOCTOR_ID NUMBER(38) NOT NULL CONSTRAINT DOCTORINFO_ID_UQ UNIQUE,
D_F_NAME VARCHAR(50) NOT NULL,
D_M_NAME VARCHAR(50) NOT NULL,
D_S_NAME VARCHAR(50) NOT NULL,
DOCTOR_NAME VARCHAR(50),
D_TITLE VARCHAR(10) NOT NULL,
D_ACTIVE_STATUS BOOLEAN NOT NULL,
SUFFIX VARCHAR(10) NOT NULL,
PASSWORD VARCHAR(50) NOT NULL,
SPECIALITY VARCHAR(35) NOT NULL,
QUALIFICATION_YEAR NUMBER(4) NOT NULL,
UNIVERSITY_NAME VARCHAR(35) NOT NULL,
HOSPITAL_NAME VARCHAR(35) NOT NULL,
D_ADDR1 VARCHAR(50),
D_ADDR2 VARCHAR(50),
USER_EMAIL_ADDRESS VARCHAR(255) NOT NULL,
D_CITY VARCHAR(50) NOT NULL,
D_STATE VARCHAR(50),
D_ZIPCODE NUMBER(6) NOT NULL,
D_HOMEPHONE NUMBER(15) NOT NULL,
D_WORKPHONE NUMBER(15) NOT NULL,
D_MOBILE NUMBER(15) NOT NULL,
START_DAYTIME TIMESTAMP,
END_DAYTIME TIMESTAMP,
START_NIGHTTIME TIMESTAMP,
END_NIGHTTIME TIMESTAMP,
D_TOKENLIMIT NUMBER(4),
D_DOB DATE NOT NULL
);
When I try to run it in Oracle 10g, I get this error:
Error code 902, SQL state 42000: ORA-00902: invalid datatype
Line 1, column 1
What is wrong with the table definition?
Your definition for column DOCTOR_ID does not conform to Oracle SQL syntax. You cannot append a named constraint to a column declaration as you attempt to do. You may do this ...
DOCTOR_ID NUMBER(38) NOT NULL,
CONSTRAINT DOCTORINFO_ID_UQ UNIQUE (DOCTOR_ID),
... to achieve the effect you seem to want. If you don't care about the specific constraint name, however, then you can also do this:
DOCTOR_ID NUMBER(38) NOT NULL UNIQUE,