Foreign key missing parenthesis - sql

CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) FOREIGN KEY REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER FOREIGN KEY REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL,
);
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
in creation of the employee table
this is giving me an error of right parenthesis
and the department table is already created

You have an extra comma in the line
SALARY DECIMAL(7,2) NOT NULL,
Delete that comma and the Employee table should be created.
You need to Create the Department Table first to use one of its
Columns as FOREIGN KEY.
Also, check your database. There might already be a Department Table. To avoid getting that error when the table needed is already created, use the keyword IF NOT EXISTS
CREATE TABLE IF NOT EXISTS Department(
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);

The error is caused by the trailing comma, but you have other issues:
The FOREIGN KEY is not needed for an inline reference.
You need to define the tables in the right order.
So . . .
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL
);
Here is an example of it working.

Related

ORA-00907: missing right parenthesis !! im trying to create tables, should i add a foreign key?

CREATE TABLE brunch
(
br_id INT NOT NULL PRIMARY KEY,
br_name INT NOT NULL
)
CREATE TABLE employee
(
e_id INT NOT NULL PRIMARY KEY,
bdate DATE,
fname VARCHAR2(30),
lname VARCHAR2(30),
sal NUMBER,
sex VARCHAR2(1),
address VARCHAR2(50),
super_id INT REFERENCES employee(e_id)
)
ALTER TABLE brunch ADD mgr_id int REFERENCES employee(e_Id);
ALTER TABLE employee ADD b_id INT REFERENCES brunch(br_id);
CREATE TABLE client
(
c_id INT NOT NULL PRIMARY KEY,
c_name VARCHAR2(40),
c_oemail VARCHAR2(40),
c_email VARCHAR2(40),
b_id reference brunch(b_id)
)
The problem is with the last table, table client, and it give me an error message that says
ORA-00907: missing right parenthesis
but I don't see any problem with the syntax. Thank you
The last DDL should be
create table client
(
c_id int primary key,
c_name varchar2(40),
c_oemail varchar2(40),
c_email varchar2(40),
b_id references brunch(br_id)
);
where column name b_id should be br_id and
there's no keyword called reference but references
P.S. There's no need to use NOT NULL for a column defined as PRIMARY KEY(already includes NOT NULL).

Foreign Key issue SQL

I cant figure out why this is not compiling It has to do with the foreign key somehow:
Drop Table Employee;
Drop Table Department;
Create Table Employee(
EmpNr int not null primary key,
EmpName Varchar(35) not null,
Dept Varchar (2) not null,
Gender char not null
);
Create Table Department(
DeptCode Varchar (2) not null primary key,
DeptName Varchar (35) not null,
Foreign Key (DeptCode) references Employee (Dept)
);
insert into Employee values (001, 'HagarT','DV','M'),
(002, 'WongS','DV','F'),
(003, 'Jones','MK','F'),
(004, 'MifuneK','SL','M');
insert into Department values ('DV', 'Development'),
('MK', 'Marketing'),
('RS', 'Research'),
('SL', 'Sales');
You need Dept be PRIMARY KEY or UNIQUE to work as FK
Create Table Employee(
EmpNr int not null primary key,
EmpName Varchar(35) not null,
Dept Varchar (2) unique not null,
Gender char not null
);
As you can see your Employee has two rows with 'DV' so which will be foreign for the Department?
I think you want is
Create Table Employee(
EmpNr int not null primary key,
EmpName Varchar(35) not null,
Dept Varchar (2) not null,
Gender char not null,
Foreign Key (Dept) references Department (DeptCode)
);

building table but get "missing right parenthesis"

I try to build this table
CREATE TABLE OFFICER
(
ID int(8) PRIMARY KEY,
FIRST_NAME varchar2(20) NOT NULL,
LAST_NAME varchar2(20) NOT NULL,
HIRE_DATE date NOT NULL,
UNHIRE_DATE date,
SALARY int(7),
PHONE_NUMBER int(10),
TYPE varchar2(15) NOT NULL
);
Do I have to use any constraint, reference? and What I lack for this code?
INT data type doesn't allow scale specification. Try either ID int primary key or Id NUMBER(8) primary key.
Try this,
CREATE TABLE OFFICER
(
ID NUMBER(8) PRIMARY KEY,
FIRST_NAME varchar2(20) NOT NULL,
LAST_NAME varchar2(20) NOT NULL,
HIRE_DATE date NOT NULL,
UNHIRE_DATE date,
SALARY NUMBER(7),
PHONE_NUMBER NUMBER(10),
TYPE varchar2(15) NOT NULL
);

i used the program SQL Fiddle and it keeps telling me that the table doesn't exist,what can i do to fix the two tables referencing each other?

the Staff table references the branch table
CREATE TABLE Staff(
StaffNo VARCHAR(5) NOT NULL,
firstName VARCHAR(15) NOT NULL UNIQUE,
lastName VARCHAR(15) NOT NULL,
position VARCHAR(10) NOT NULL,
salary INTEGER
DEFAULT 3000,
CHECK (salary BETWEEN 3000 AND 25000),
email VARCHAR(25),
branchNo CHAR(6) NOT NULL,
PRIMARY KEY (StaffNo),
FOREIGN KEY (branchNo) REFERENCES Branch (branchNo));
and at the same time the branch table references the Staff table
create table Branch(
branchNo char(6) not null primary key,
street varchar(30) not null,
city varchar(20),
postCode char(5) not null,
ManagerNo varchar(5) not null,
foreign key (ManagerNo) references Staff(StaffNo));
Since your tables reference each other in the Foreign Keys you will get an error on either table creation if the other table has not been created yet. I would suggest that you remove the creation of the FOREIGN KEYs to separate ALTER TABLE statements:
CREATE TABLE Staff(
StaffNo VARCHAR(5) NOT NULL,
firstName VARCHAR(15) NOT NULL UNIQUE,
lastName VARCHAR(15) NOT NULL,
position VARCHAR(10) NOT NULL,
salary INTEGER
DEFAULT 3000,
CHECK (salary BETWEEN 3000 AND 25000),
email VARCHAR(25),
branchNo CHAR(6) NOT NULL,
PRIMARY KEY (StaffNo)
);
create table Branch(
branchNo char(6) not null primary key,
street varchar(30) not null,
city varchar(20),
postCode char(5) not null,
ManagerNo varchar(5) not null
);
alter table staff
add constraint fk1_branchNo foreign key (branchNo) references Branch (branchNo);
alter table branch
add constraint fk1_ManagerNo foreign key (ManagerNo) references Staff (StaffNo);
See SQL Fiddle with Demo
You can remove one reference from one table and keep the other.then you can retrieve data using the remainig reference.Is there any problem with that?

ORA-00900: invalid SQL statement

CREATE TABLE Customers(
CustID number(5,0),
EmpID CHAR(1),
Cust_Name varchar(20) not null,
Cust_Address varchar(20) not null,
Cust_City varchar(20) not null,
Cust_State char(2) not null,
Cust_Zipcode number(5,0) not null,
Ship_Date date not null,
Order_Date date not null,
constraint ci_fk FOREIGN KEY (EmpID) references EMPLOYEES(EmpID),
constraint ci_ck check (Ship_Date>Order_Date)
)
What's the problem?
Employees table does not exist.
or EmpId is not a primary key.
Once I did these, my copy of the create statement worked.
Chris said it.
Change CHAR to VARCHAR2 as CHAR should never be used. Also, number(5,0) is the same as NUMBER(5), so you can use that.
Verify that the Employees table exists.
Verify that the EmpID column in the Employees table is of the same datatype as in the Customers table.
Verify that the EmpID column in the Employees table is the primary key of the employee table.