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);
Related
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)
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 )
How do I look at one table and use it to check another table for data integrity?
I have two SQL Tables.
One that is a person table
CREATE TABLE PERSON
(
ID INT IDENTITY(10000,1) NOT NULL,
Firstname VARCHAR(15),
Lastname VARCHAR(25) NOT NULL,
Birthdate DATE,
Gender VARCHAR(1),
CHECK ( GENDER IN ('M', 'F')),
Street VARCHAR(50),
City VARCHAR(15),
State VARCHAR(2),
CHECK (State IN ('FL','GA','PA')),
Zip INT,
Phone VARCHAR(10),
Employee VARCHAR(1),
CHECK ( Employee IN('Y','N')),
Member VARCHAR(1),
CHECK ( Member IN('Y','N')),
CHECK (Member IN ('Y') or Employee IN ('Y')),
CONSTRAINT PERSON_PK PRIMARY KEY (ID));
And Employee Table
CREATE TABLE EMPLOYEE
(
ID INT NOT NULL,
Datehired DATE DEFAULT GETDATE(),
Status VARCHAR(1),
CHECK ( Status IN ('F','C')),
Position VARCHAR(25),
EmpType VARCHAR(25),
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (ID),
CONSTRAINT EMPLOYEE_PERSON_FK FOREIGN KEY (ID) REFERENCES PERSON);
Let's say someone isn't an employee. I can still insert them into the Employee Table.
INSERT INTO EMPLOYEE
(ID, Status, Position, EmpType)
VALUES
('10000','C','Teaching Classes','Instructor');
How Do I prevent this from happening.
One method is to have a redundant key:
alter table person
contraint unq_person_id_employee
unique (id, employee);
Then add a computed column to employee:
alter table employee add employee as ('Y') persisted;
Finally, add the constraint:
alter table employee
add constraint fk_employee_person
foreign key (id, employee) references person(id, employee);
Now, you are guaranteed that only employees are in the Employee table.
SQL Error
I'm not sure what I did wrong here
Here is the DDL I used to create my tables
Create Table HomeState (StateAbbreviation char(2) Primary Key,
StateName varchar(25));
Create Table Country (CountryAbbreviation char(2) Primary Key,
CountryName varchar(35));
Create Table Employee (EmployeeID Integer Primary Key NOT NULL,
FirstName varchar(20),
LastName varchar(30),
MI char(1),
HomeAddress varchar(30),
Zip char(5),
DateOfBirth date,
HireDate date,
TerminationDate date,
AnnualSalary number(20,2),
LicenseDate date,
StateAbbreviation char(2),
CountryAbbreviation char(2),
Foreign Key (StateAbbreviation) references HomeState,
Foreign Key (CountryAbbreviation) references Country);
Create Table Truck (VinNumber Integer Primary Key,
Make varchar(25),
Model varchar(30),
Year Integer,
PurchasePrice number(20,2),
LicenseNumber varchar(15));
Create Table EmployeeTruck (EmployeeID Integer,
VinNumber Integer,
Primary Key(EmployeeID,VinNumber),
Foreign Key (EmployeeID) references Employee,
Foreign Key (VinNumber) references Truck);
Create Table Accident (AccidentID Integer Primary Key,
DateOfAccident date,
AccidentDescription varchar(200),
AccidentLocation varchar(100),
EmployeeID Integer,
Foreign Key (EmployeeID) references Employee);
and here is the command i used to try and fill in the employee table
insert into employee
values ('1','brian','kim','j','adfasdf',
'1234','24-nov-1993','24-sep-1993','24-sep-1993',
'1234','24-sep-1993','as','as')
but it always gives me the error i put as the title of this question...
In the table "Employee", you say that Foreign Key (StateAbbreviation) references HomeState, and Foreign Key (CountryAbbreviation) references Country). The values you insert into Employee.HomeState and Employee.CountryAbbreviation have to exist in the tables HomeState and Country before you can insert into Employee.
Insert rows into HomeState and Country before you insert into Employee.
You have other problems, too. Here's an example.
Create Table HomeState (StateAbbreviation char(2) Primary Key,
StateName varchar(25));
insert into HomeState values ('AL', 'Alabama');
insert into HomeState values ('AM', 'Alabama');
insert into HomeState values ('AN', 'Alabama');
All those insert statements succeed. They shouldn't. In this case, StateName is also a candidate key.
Create Table HomeState (
StateAbbreviation char(2) Primary Key,
StateName varchar(25) not null unique
);
Let's take this a little further.
Create Table HomeState (
StateAbbreviation char(2) Primary Key,
StateName varchar(25) not null unique
);
Create Table Country (
CountryAbbreviation char(2) Primary Key,
CountryName varchar(35) not null unique
);
insert into HomeState values ('CA', 'California');
insert into Country values ('AF', 'Afghanistan');
insert into Employee (EmployeeID, StateAbbreviation, CountryAbbreviation)
values (-42, 'CA', 'AF');
The state "California, US" makes sense. The state "California, Afghanistan" doesn't.
An employee having no name doesn't make sense. Declare FirstName, LastName, and HireDate not null.
Bet on this: whatever nonsense your database allows will appear. It's just a matter of time.
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)