Turn existing column into a primary key - sql

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)

Related

There are no primary or candidate keys in the referenced table 'tblgender' that match the referencing column list in the foreign key 'giFK'

create database my_data
create table tblperson(
pid int primary key,
pname varchar(15),
pgender varchar(15)
)
create table tblgender(
gid varchar(15) primary key,
gender varchar(15)
)
Alter table tblperson add constraint giFK
Foreign key (pgender) references tblgender(gid)

Alter table structure without violating primary key and unique constraints

Here is a logical model that I have.
Below are table structures and their constraints
Doctor
CREATE TABLE doctor (
doctor_id NUMBER(4) NOT NULL,
doctor_title VARCHAR2(2) NOT NULL,
doctor_fname VARCHAR2(50),
doctor_lname VARCHAR2(50),
doctor_phone CHAR(10) NOT NULL
);
ALTER TABLE doctor ADD CONSTRAINT doctor_pk PRIMARY KEY ( doctor_id );
Procedure
CREATE TABLE procedure (
proc_code NUMBER(5) NOT NULL,
proc_name VARCHAR2(100) NOT NULL,
proc_description VARCHAR2(300) NOT NULL,
proc_time NUMBER(3) NOT NULL,
proc_std_cost NUMBER(7, 2) NOT NULL
);
ALTER TABLE procedure ADD CONSTRAINT procedure_pk PRIMARY KEY ( proc_code );
ALTER TABLE procedure ADD CONSTRAINT proc_name_unq UNIQUE ( proc_name );
Admission
CREATE TABLE admission (
adm_no NUMBER(6) NOT NULL,
adm_date_time DATE NOT NULL,
adm_discharge DATE,
patient_id NUMBER(6) NOT NULL,
doctor_id NUMBER(4) NOT NULL
);
ALTER TABLE admission ADD CONSTRAINT admission_pk PRIMARY KEY ( adm_no ); --surrogate key
ALTER TABLE admission ADD CONSTRAINT admission_nk UNIQUE ( patient_id,
adm_date_time );
Admission Procedure
CREATE TABLE adm_prc (
adprc_no NUMBER(7) NOT NULL,
adprc_date_time DATE NOT NULL,
adprc_pat_cost NUMBER(7, 2) NOT NULL,
adprc_items_cost NUMBER(6, 2) NOT NULL,
adm_no NUMBER(6) NOT NULL,
proc_code NUMBER(5) NOT NULL,
request_dr_id NUMBER(4) NOT NULL,
perform_dr_id NUMBER(4)
);
ALTER TABLE adm_prc ADD CONSTRAINT adm_prc_pk PRIMARY KEY ( adprc_no ); --surrogate key
ALTER TABLE adm_prc ADD CONSTRAINT adm_prc_nk UNIQUE ( adprc_date_time,
adm_no );
ALTER TABLE adm_prc
ADD CONSTRAINT admission_admprc FOREIGN KEY ( adm_no )
REFERENCES admission ( adm_no );
ALTER TABLE adm_prc
ADD CONSTRAINT doctor_performadmprc FOREIGN KEY ( perform_dr_id )
REFERENCES doctor ( doctor_id );
ALTER TABLE adm_prc
ADD CONSTRAINT doctor_requestadmprc FOREIGN KEY ( request_dr_id )
REFERENCES doctor ( doctor_id );
Item Treatment
CREATE TABLE item_treatment (
adprc_no NUMBER(7) NOT NULL,
item_code CHAR(5) NOT NULL,
it_qty_used NUMBER(2) NOT NULL,
it_item_total_cost NUMBER(8, 2) NOT NULL
);
ALTER TABLE item_treatment
ADD CONSTRAINT item_treatment_pk PRIMARY KEY ( adprc_no,item_code);
ALTER TABLE item_treatment
ADD CONSTRAINT admprc_itemtreatment FOREIGN KEY ( adprc_no )
REFERENCES adm_prc ( adprc_no );
ALTER TABLE item_treatment
ADD CONSTRAINT admprc_itemtreatment FOREIGN KEY ( adprc_no )
REFERENCES adm_prc ( adprc_no );
ALTER TABLE item_treatment
ADD CONSTRAINT item_itemtreatment FOREIGN KEY ( item_code )
REFERENCES item ( item_code );
In a fictitious hospital, every time an admission procedure is completed for a patient admission, the lead doctor who performed the procedure is recorded under the perform_dr_id in adm_prc table. Even if a team of doctors perform the procedure, only the lead doctor is recorded.
The hospital now wishes to record all the doctors who performed in the admission procedure including an Ancillary doctor (assisting doctor).
I have altered the adm_prc table to include ancillary doctors
ALTER TABLE ADM_PRC ADD ANCILLARY_DR_ID NUMBER(4);
ALTER TABLE ADM_PRC
ADD CONSTRAINT DOCTOR_PERFORM_ANCILLARY FOREIGN KEY ( ANCILLARY_DR_ID )
REFERENCES DOCTOR ( DOCTOR_ID );
I need to change the structure of the database is such a way that, there should be multiple records for the same admission procedure as there can be many doctors assisting in a single admission procedure. For example, I should be able to insert the following records in adm_prc table
adprc_no adprc_date_time adprc_pat_cost adprc_items_cost adm_no proc_code
request_dr_id perform_dr_id ancillary_dr_id
-----------------------------------------------------------------------------------------------------------------------------
1 14/03/2019 100 100 1234 1234
10 10 12
1 14/03/2019 100 100 1234 1234
10 10 13
However, this violates primary key constraint adm_prc_pk
I'm stuck at this point and unable to proceed further. Would appreciate if someone could point me in the right direction.
You should create a new satellite table (say ADM_PRC_DR_DTLS) to hold adprc_no and corresponding dr_id details.
adprc_no of ADM_PRC_DR_DTLS would refer to the ADM_PRC's adprc_no as a Foreign Key.

update a select query in oracle DB

I have 5 tables in a DB like this (script below)::
DB SCRIPT
CREATE TABLE EQUIPE (
code_equipe char(3) primary key,
nom varchar(30),
directeur varchar(30));
CREATE TABLE PAYS (
code_pays varchar(3) primary key,
nom varchar(20));
CREATE TABLE COUREUR (
num_dossart number(3) primary key,
code_equipe char(3),
nom varchar(30),
code_pays varchar(3));
CREATE TABLE ETAPE (
num_etape number(1) primary key,
date_etape date,
kms number(3),
ville_depart varchar(20),
ville_arrivee varchar(20));
CREATE TABLE TEMPS (
num_dossart number(3),
num_etape number(1),
temps_realise number(6),
primary key(num_dossart,num_etape));
ALTER table COUREUR add CONSTRAINT FK_avoir_code_equipe FOREIGN KEY (code_equipe) REFERENCES EQUIPE(code_equipe);
ALTER table COUREUR add CONSTRAINT FK_avoir_code_pays FOREIGN KEY (code_pays) REFERENCES PAYS(code_pays);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_dossart FOREIGN KEY (num_dossart) REFERENCES COUREUR(num_dossart);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_etape FOREIGN KEY (num_etape) REFERENCES ETAPE(num_etape);
and my query
select num_etape,max(temps_realise) from TEMPS group by num_etape
gave this result
and i want to update it to give result like this
this is
If I read it correctly, a simple join does the job:
select t.num_etape,
c.nom,
max(n.temps_realise)
from temps t join coureur c on c.num_dossart = t.num.dossart
group by t.num_etape,
c.nom;
I have solved my question thanks guys
select num_dossart,nom,num_etape,dernier from COUREUR C,
(select num_etape ,max(temps_realise) as dernier from TEMPS GROUP BY num_etape) T
where num_dossart =
(select num_dossart from TEMPS where temps_realise = dernier )

SQL Server - Adding Primary and Foreign Keys

I have one query that creates several tables, listed below, and a second query that is supposed to add Primary and Foreign keys to each. However, when I try executing my query, I'm greeted with the following error messages:
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'DEPARTMENT' that match the referencing column list in the foreign key 'FK__EMPLOYEE__dno__25869641'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
I'm not sure what this means, as I have researched both primary and foreign keys. Can anyone lead me in the right direction as to how to solve this issue? Thanks! :)
CREATE TABLE EMPLOYEE
(
fname varchar(100),
minit char(1),
lname varchar(100),
ssn char(9),
bdate date,
addr varchar(100),
sex char(1),
salary int,
super_ssn char(9),
dno int
);
CREATE TABLE DEPARTMENT
(
dname varchar(100),
dnumber int,
mgr_ssn char(9),
mgr_start_date date
);
CREATE TABLE DEPENDENTS
(
essn char(9),
dependent_name varchar(100),
sex char(1),
bdate date,
relationship varchar(100)
);
CREATE TABLE DEPT_LOCATIONS
(
dnumber int,
dlocation varchar(100)
);
CREATE TABLE PROJECT
(
pname varchar(100),
pnumber int,
plocation varchar(100),
dnum int
);
CREATE TABLE WORKS_ON
(
essn char(9),
pno int,
hrs float
);
ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);
ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
Primary key column should be non null able. Table definition should have not null for all PKs.
CREATE TABLE EMPLOYEE (
fname varchar(100),
minit char(1),
lname varchar(100),
ssn char(9) not null,
bdate date,
addr varchar(100),
sex char(1),
salary int,
super_ssn char(9),
dno int
);
CREATE TABLE DEPARTMENT (
dname varchar(100),
dnumber int not null,
mgr_ssn char(9),
mgr_start_date date
);
CREATE TABLE DEPENDENTS (
essn char(9) not null,
dependent_name varchar(100) not null,
sex char(1),
bdate date,
relationship varchar(100)
);
CREATE TABLE DEPT_LOCATIONS (
dnumber int not null,
dlocation varchar(100) not null
);
CREATE TABLE PROJECT (
pname varchar(100),
pnumber int not null,
plocation varchar(100),
dnum int
);
CREATE TABLE WORKS_ON (
essn char(9) not null,
pno int not null,
hrs float
);
ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);
ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);

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.