How to make relation between two tables in sql - 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.

Related

SQL create 2 tables with constraints

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);

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.

ORA-00957 duplicate column name error, when trying to reference the same primary key with 3 foreign keys

I'm having problems with creating tables:
CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);
CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),
CONSTRAINT fk_leader
FOREIGN KEY (consultant_leader, developer_leader, project_leader)
REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);
In the last section, when I try to reference the employee's table employee_id, I'm getting ORA-00957. I think it's because the 3 different leader type foreign key references the same employee_id, but as far as I know, it should not be a problem. Is the syntax wrong?
Your immediate problem is that you need three foreign key relationships, not one with three columns.
But, there is no need to declare a primary key as being unique. So, I would recommend:
CREATE TABLE EMPLOYEE (
employee_id NUMBER(5) NOT NULL PRIMARY KEY,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
);
CREATE TABLE PROJECT (
project_id NUMBER(5) NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (project_leader)
REFERENCES EMPLOYEE (employee_id)
);
You don't need to put the PRIMARY KEY constraint in-line, of course. The advantage of declaring it separately is that you can give the constraint a name to your liking.
I think you should create three distinct FK: FK_Consultant, FK_developer, FK_projleader

Turn existing column into a primary key

create table emp
(
emp_id int,
emp_name varchar(40) not null,
emp_address varchar(35) not null,
)
Now I need to add primary key to emp_id . I tried using alter query
alter table emp add constraint emp_id primary key
its showing error as
Table level constraint does not specify column list, table 'emp'.
ALTER TABLE emp
ADD PRIMARY KEY (emp_id)
OR
ALTER TABLE emp
ADD CONSTRAINT pk_empID PRIMARY KEY (emp_id)