SQL create 2 tables with constraints - sql

Starting with first table: create the two tables with constraints
CREATE TABLE department
(
depid varchar2(3) CONSTRAINT PKdepid PRIMARY KEY,
dname varchar2(10) NOT NULL
);
First table is created.
Starting second table:
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAINT FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
);
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAINT FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
)
ERROR at line 1:
ORA-02253: constraint specification not allowed here
And in second condition applied
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAIN FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
);
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAIN FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
)
ERROR at line 1:
ORA-00907: missing right parenthesis
I just wanted to create table

You shouldn't specify foreign key in an inline foreign key definition, just references (not reference as you currently have). Also, note your statement references the table dep while your other table is in fact called department:
CREATE TABLE employee (
eid NUMBER CONSTRAINT pkeid PRIMARY KEY,
ename VARCHAR2(10),
depid VARCHAR2(3) CONSTRAINT fkdepid REFERENCES department(depid), -- Here!
designation VARCHAR2(10),
salary NUMBER CHECK(salary>10000),
doj DATE);

Related

The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "EMP_SELF_KEY"

I have an employee table like this
CREATE TABLE EMP (
EMPNO int NOT NULL,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR int CONSTRAINT EMP_SELF_KEY REFERENCES EMP (EMPNO),
HIREDATE DATE,
SAL int,
COMM int,
DEPTNO int NOT NULL,
CONSTRAINT EMP_FOREIGN_KEY FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO),
CONSTRAINT EMP_PRIMARY_KEY PRIMARY KEY (EMPNO));
and i tried to delete
employees with empno
DELETE FROM EMP WHERE EMPNO =7566
but it's giving me an error like this:-
The DELETE statement conflicted with the SAME TABLE REFERENCE
constraint "EMP_SELF_KEY". The conflict occurred in database
"training", table "dbo.EMP", column 'MGR'. The statement has been
terminated.
so what should I do to delete the employee?
fiddle link
code

Foreign key missing parenthesis

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.

oracle error when second table

CREATE TABLE faculty1(
f_num varchar2(5),
f_name varchar2(20),
rank varchar2(30),
d_name char(3),
salary number(8,2),
PRIMARY KEY(f_name)
);
Table created
CREATE TABLE class1(
c_name varchar2(10),
c_time varchar2(10),
f_name varchar2(20),
c_room varchar2(10),
semester varchar2(10),
PRIMARY KEY(c_name, c_time),
FOREIGN KEY(f_name) REFERENCES faculty1(f_name)
);
error at line 8
ORA-02270: no matching unique or primary key for this column-list
What am I doing wrong?
CREATE TABLE grade1(
s_name varchar2(10),
c_name varchar2(10),
grade char(1),
PRIMARY KEY(s_name,c_name),
FOREIGN KEY(c_name) REFERENCES class1(c_name)
);
same error as before
f_name should be primary key in table faculty1 to be referenced as a foreign key in class1 table.
SQLFiddle

Oracle SQL crazy subqueries across 4 tables

Here are the relations:
CREATE TABLE employee (
fname varchar2(15) not null,
minit varchar2(1),
lname varchar2(15) not null,
ssn char(9),
bdate date,
address varchar2(30),
sex char,
salary number(10,2),
superssn char(9),
dno number(4),
primary key (ssn),
foreign key (superssn) references employee(ssn),
foreign key (dno) references department(dnumber)
);
CREATE TABLE department (
dname varchar2(15) not null,
dnumber number(4),
mgrssn char(9) not null,
mgrstartdate date,
primary key (dnumber),
unique (dname),
foreign key (mgrssn) references employee(ssn)
);
CREATE TABLE dept_locations (
dnumber number(4),
dlocation varchar2(15),
primary key (dnumber,dlocation),
foreign key (dnumber) references department(dnumber)
);
CREATE TABLE project (
pname varchar2(15) not null,
pnumber number(4),
plocation varchar2(15),
dnum number(4) not null,
primary key (pnumber),
unique (pname),
foreign key (dnum) references department(dnumber)
);
CREATE TABLE works_on (
essn char(9),
pno number(4),
hours number(4,1),
primary key (essn,pno),
foreign key (essn) references employee(ssn),
foreign key (pno) references project(pnumber)
);
Q: Find the names and addresses of all employees who work on at least one project located in Houston but whose department has no location in Houston.
Here is my query:
select fname, minit, lname, address
from employee
where dno=
(select dnumber from dept_locations where dlocation<>'Houston' and dnumber=
(select dno from employee where ssn=
(select essn from works_on where pno=
(select pnumber from project where plocation='Houston'))))
But it did not compile and returned this error:
ORA-01427: single-row subquery returns more than one row
Please help!
select fname, minit, lname, address
from employee
where dno in
(select dnumber from dept_locations where dlocation<>'Houston') and ssn in
(select essn from works_on where pno in
(select pnumber from project where plocation='Houston'))))
Your call "where dno=" expects a single result.
try
where dno in (select...)
select
fname,
minit,
lname,
address
from
employee
where
exists (
select null
from works_on join project on (works_on.pno = project.pnumber)
where works_on.essn = employee.ssn and
not exists (
select null
from dept_locations
where dept_locations.dlocation = project.plocation))
Other comments:
The lack of consistency in table and column naming is very irritating to work with. DNO and DNUMBER?
Using a social security number as a key value is a stupid error. It's stupid because people's SSN's are private and should be protected, and it's an error because it is not immutable.

How to make relation between two tables in sql

I want to make relation between these two tables, I have created two tables emp and dept as shown below,
create table emp (
empno number(4) constraint empnopk primary key,
ename varchar(10) constraint enamenn not null,
mgrno number(4) constraint mgrnofk references emp(empno),
sal number(10) constraint salck check(sal between 2000 and 5000),
age number(2) constraint ageck check(age>20),
mobno number(10) constraint mobuq unique
);
create table dept (
deptno number(10) constraint deptnopk primary key,
dname varchar(10) constraint dnamenn not null,
loc varchar(10) default 'xyz'
);
as the emp table is the master table and dept table is child table,to make relation between these two tables i have given query
alter table emp
add constraint deptnofk foreign key(deptno) references dept(deptno);
when i give this query it is showing ORA-00904:"DEPTNO": invalid identifier
What may be the problem?
Your emp table does not have a deptno field. Add this as a number(10) and it should work better.
You need add deptno field in your emp table, because you are creating a foreign key from table emp to dept by droptno field.