creating table in in cmd [duplicate] - sql

This question already has answers here:
SQL - Missing right parenthesis
(4 answers)
Closed 7 years ago.
create table reservation
(
reservationid varchar2(6) primary key,
userid varchar2(6) foreign key references userprofile(userid),
vehicleid varchar2(6) foreign key references vehicle(vehicleid),
routeid varchar2(8) foreign key references route(routeid),
bookingdate date not null,
journeydate date not null,
driverid varchar2(6) foreign key references driver(driverid),
bookingstatus varchar2(20) not null,
totalfare number(10) not null,
boardingpoint varchar2(30) not null,
droppoint varchar2(30) not null,
vname varchar2(20) not null
);
I am getting an error:
ERROR at line 1:
ORA-00907: missing right parenthesis

You don't need the foreign key for an inline reference. This code works in SQL Fiddle:
create table vehicle (vehicleid varchar2(6) primary key);
create table userprofile (userid varchar2(6) primary key);
create table route (routeid varchar2(8) primary key);
create table driver (driverid varchar2(6) primary key);
create table reservation
(
reservationid varchar2(6) primary key,
userid varchar2(6) references userprofile(userid) ,
vehicleid varchar2(6) references vehicle(vehicleid),
routeid varchar2(8) references route(routeid),
bookingdate date not null,
journeydate date not null,
driverid varchar2(6) references driver(driverid),
bookingstatus varchar2(20) not null,
totalfare number(10) not null,
boardingpoint varchar2(30) not null,
droppoint varchar2(30) not null,
vname varchar2(20) not null
);

Related

Oracle returns ORA-00904: invalid identifier error when adding foreign keys

I am unsure as to why I can't insert org_id and vol_id as foreign keys. It returns the error in the title ORA-00904: invalid identifier
CREATE TABLE VOLUNTEERS (
vol_id varchar2(3) not null,
vol_fname varchar2(10) not null,
vol_lname varchar2(10) not null,
vol_address varchar2(20) not null,
vol_telephone varchar2(10) not null,
primary key (vol_id));
CREATE TABLE ORGANIZATIONS (
org_id varchar2(3) not null,
org_name varchar2(30) not null,
org_contact varchar2(20) not null,
org_address varchar2(20) not null,
org_telephone varchar2(10) not null,
primary key (org_id));
alter table volunteers
add foreign key (org_id) references ORGANIZATIONS;
alter table organizations
add foreign key (vol_id) references VOLUNTEERS;
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (col_name) REFERENCES table_name(col_name);
A foreign key constraint is a constraint on an existing column. You have not defined the columns in each of the tables:
CREATE TABLE VOLUNTEERS (
vol_id varchar2(3) not null,
vol_fname varchar2(10) not null,
vol_lname varchar2(10) not null,
vol_address varchar2(20) not null,
vol_telephone varchar2(10) not null,
org_id varchar2(3),
------^
primary key (vol_id)
);
CREATE TABLE ORGANIZATIONS (
org_id varchar2(3) not null,
org_name varchar2(30) not null,
org_contact varchar2(20) not null,
org_address varchar2(20) not null,
org_telephone varchar2(10) not null,
vol_id varchar2(3),
------^
primary key (org_id)
);
Once the columns are defined, the foreign key constraints will work.

How to fix invalid data type size in this sql query?

The query i wrote as
CREATE TABLE Shopper (
Shopperid NUMBER(4) CONSTRAINT shpr_shprid_pk PRIMARY KEY,
ShopperName VARCHAR2(20) CONSTRAINT shpr_shprname_nn NOT NULL,
Gender CHAR(6) CONSTRAINT shpr_gdr_chk CHECK(Gender IN ('Male','Female')),
MobileNo NUMBER(10) CONSTRAINT shpr_mobno_nn NOT NULL,
Address VARCHAR2(50)
);
The issue is with NUMBER type definitions, try:
CREATE TABLE Shopper (
Shopperid NUMBER CONSTRAINT shpr_shprid_pk PRIMARY KEY,
ShopperName VARCHAR2(20) CONSTRAINT shpr_shprname_nn NOT NULL,
Gender CHAR(6) CONSTRAINT shpr_gdr_chk CHECK(Gender IN ('Male','Female')),
MobileNo NUMBER CONSTRAINT shpr_mobno_nn NOT NULL,
Address VARCHAR2(50)
);

what's wrong with writing the code: ORA-00907: missing right parenthesis

CREATE TABLE comenzi
(
id_comanda NUMBER(5) CONSTRAINT id_comanda_pk PRIMARY KEY,
nume_companie VARCHAR2(40) NOT NULL,
persoana_contact VARCHAR2(40) NOT NULL,
data_comanda DATE,
data_expediere DATE,
loc_expediere VARCHAR2(24) UNIQUE;
)
CREATE TABLE detalii_comenzi
(
id_comanda NUMBER(5) CONSTRAINT id_comanda_fk REFERENCES comenzi(id_comenzi),
id_produs NUMBER(5) CONSTRAINT id_produs_pk PRIMARY KEY,
pret_unitar NUMBER(7),
cantitate NUMBER(7) NOT NULL;
)
You are not using right data types. Please try using below code.
CREATE TABLE #comenzi
(
ID_COMANDA INT CONSTRAINT id_comanda_pk PRIMARY KEY,
NUME_COMPANIE VARCHAR(40) NOT NULL,
PERSOANA_CONTACT VARCHAR(40) NOT NULL,
DATA_COMANDA DATE,
DATA_EXPEDIERE DATE,
LOC_EXPEDIERE VARCHAR(24) UNIQUE
)
CREATE TABLE detalii_comenzi
(
id_comanda INT CONSTRAINT id_comanda_fk REFERENCES #comenzi (ID_COMANDA),
id_produs INT CONSTRAINT id_produs_pk PRIMARY KEY,
pret_unitar INT,
cantitate INT NOT NULL
)

SQL Error: ORA-00904: "CATEGORYID": invalid identifier"

CREATE TABLE CUSTOMER1
(
CUSTOMERID VARCHAR2(2) NOT NULL,
FIRSTNAME VARCHAR2(50) NOT NULL,
LASTNAME VARCHAR2(50) NOT NULL,
PHONENUMBER VARCHAR2(50) NOT NULL,
BIRTHDATE DATE NOT NULL,
DRIVERLICENSENUMBER NUMBER,
STATUS VARCHAR2(50),
CREDITCARENUMBER NUMBER,
CONSTRAINT CustomerID_PK PRIMARY KEY(CUSTOMERID)
);
CREATE TABLE CCATEGORY
(
CATEGORYID INT NOT NULL,
CATEGORYNAME VARCHAR2(50) NOT NULL,
CONSTRAINT categoryID_pk PRIMARY KEY (CATEGORYID)
);
CREATE TABLE TAPE
(
TAPEID NUMBER(5) NOT NULL,
TAPETITLE VARCHAR2(200) NOT NULL,
RELEASEYEAR NUMBER(5) NOT NULL,
DATEPURCHASED DATE NOT NULL,
PRICE NUMERIC(5) NOT NULL,
RENTEDOUT VARCHAR2(20) NOT NULL,
RATING VARCHAR2(10) NOT NULL,
ACTIONONRETURN VARCHAR2(50) NOT NULL,
PRIMARY KEY(TAPEID),
CONSTRAINT FK_CATEGORYID FOREIGN KEY(CATEGORYID) REFERENCES CCATEGORY(CATEGORYID),
CONSTRAINT FK_RESERVEDBY FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER(CUSTOMERID)
);
Customer, and CCategory table are created. When I try to create TAPE table I get the following error: SQL Error: ORA-00904: "CATEGORYID": invalid identifier. Can't seem to figure out why. I created the customer table first, then the category table before trying to create the TAPE table.
Problem is that you have not defined CATEGORYID and CUSTOMERID in TAPE table. Add it and it would work.
Complete statement would be
CREATE TABLE TAPE
(
TAPEID NUMBER(5) NOT NULL,
TAPETITLE VARCHAR2(200) NOT NULL,
RELEASEYEAR NUMBER(5) NOT NULL,
DATEPURCHASED DATE NOT NULL,
PRICE NUMERIC(5) NOT NULL,
RENTEDOUT VARCHAR2(20) NOT NULL,
RATING VARCHAR2(10) NOT NULL,
ACTIONONRETURN VARCHAR2(50) NOT NULL,
CATEGORYID INT NOT NULL,
CUSTOMERID VARCHAR2(2) NOT NULL,
CONSTRAINT tape_primary_key PRIMARY KEY(TAPEID),
CONSTRAINT FK_CATEGORYID FOREIGN KEY(CATEGORYID) REFERENCES CCATEGORY(CATEGORYID),
CONSTRAINT FK_RESERVEDBY FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER1(CUSTOMERID)
);

Strange problems while setting foreign key

Database: Oracle 10.1 on os:xp2002
I am setting foreign key in employee table but it I get an error
no matching unique or primary key for this column-list
I am using following queries for creating the tables.
Department table:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4));
Employee table:
create table employee(
e_id number(4) ,
e_name varchar2(30) not null,
e_f_name varchar2(30) not null,
e_desg varchar2(20) not null,
e_address varchar2(50) not null,
e_phone_no number(12) not null,
e_salary number(10) not null,
e_house_rent number(6) not null,
e_conv_allow number(6) not null,
e_email varchar2(50) not null unique,
d2_name varchar2(10) not null,
e_hire_month number(2) not null,
e_hire_year number(4) not null,
constraint e_id_pk primary key(e_id),
constraint d2_name_fk foreign key(d2_name) references department(d_name))
;
Any solution please.
the foreign key must be a primary or unique key in the other table.
Make d_name PRIMARY KEY of Department table.
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4),
constraint d_name_pk primary key(d_name));
And then create the Employee table.
From what you've posted you don't seem to have a primary key on the department table. Something like:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4)
constraint department_pk primary key(d_name));
Or after creation:
alter table department
add constraint department_pk primary key(d_name)
...