I have created my tables and populated it but I am not sure how to write a query to verify the referential integrity across all tables.
This is what I need:
SELECT lists all primary keys
FROM lists all tables
WHERE shows all relationships
This is what I have so far:
SELECT
categId, zipCode, suppId, acctId, prodId, orderId
From
CATEGORY, ZIP, SUPPLIER, ACCOUNT, SUPPLIER_REGION, PRODUCT, PROD_DETAIL, ORDERX, ORDER_LINE_ITEM
I am not sure what to put for WHERE.
My professor wants a command to verify the referential integrity. There are a lot of other stuff he wants but this is the only thing I am not sure how to do.
I added my tables.
create table CATEGORY (
categId varchar (8),
categIdParent varchar (8),
constraint CATEGORY_PK primary key (categId),
constraint CATEGORY_FK foreign key (categIdParent) references CATEGORY (categId)
);
create table ZIP(
zipCode int (5),
city varChar (16),
state varChar (16),
constraint ZIP_PK primary key (zipCode)
);
create table SUPPLIER (
supplId int (9),
supplName varChar (24),
supplStatus varChar (16),
supplYearEstabl date,
zipCode int (5),
constraint SUPPLIER_PK primary key (supplId),
constraint SUPPLIER_FK Foreign Key (zipCode) references ZIP (zipCode)
);
CREATE TABLE SUPPLIER_REGION (
supplId int (9),
region varChar (24),
constraint SUPPLIER_REGION_PK primary key (supplId, region),
constraint SUPPLIER_REGION_FK foreign key (supplId) references SUPPLIER (supplId)
);
create table PRODUCT (
prodId int (11),
prodDescr varChar (256),
prodType varChar (12),
prodModel Char (16),
prodPlaceOrigin Char (16),
prodPrice decimal (10,2),
prodMinQty int (8),
supplId int (9),
categId varchar (8),
constraint PRODUCT_PK primary key (prodId),
constraint PRODUCT_FK1 foreign key (supplId) references SUPPLIER (supplId),
constraint PRODUCT_FK2 foreign key (categId) references CATEGORY (categId)
);
create table PROD_DETAIL (
prodId int (11),
prodDetailNo int (8),
prodDetailName varChar (32),
prodDetailValue decimal (10,2),
constraint PROD_DETAIL_PK primary key (prodID, prodDetailNo),
constraint PROD_DETAIL_FK foreign key (prodID) references PRODUCT (prodID)
);
create table ACCOUNT (
acctId varchar(50),
acctName varChar (24),
acctDept varChar (16),
acctTitle varChar (16),
acctGender Char (1),
acctEmail varChar (24),
acctAddr varChar (24),
zipCode int (5),
acctPhone int (11),
constraint ACCOUNT_PK primary key (acctId),
constraint ACCOUNT_FK foreign key (zipCode) references ZIP (zipCode)
);
create table ORDERX (
orderId int (8),
orderPayMethod varChar (16),
orderShipDate date,
acctId varchar (50),
constraint ORDER_PK primary key (orderId),
constraint ORDER_FK foreign key (acctId) references ACCOUNT (acctId)
);
create table ORDER_LINE_ITEM (
orderId int (8),
orderLineNo int (8),
orderLineQty int (8),
orderLineUnit int (8),
orderLinePrice decimal (10,2),
prodId int (11),
constraint ORDER_LINE_ITEM_PK primary key (orderId, orderLineNo),
constraint ORDER_LINE_ITEM_FK1 foreign key (orderId) references ORDERX (orderId),
constraint ORDER_LINE_ITEM_FK2 foreign key (prodId) references PRODUCT (prodId)
);
For SQL Server one good way is to create a diagram in SSMS. As you place each table into the diagram the FK relationships will be rendered as lines between tables and primary keys will be displayed as "key" icons.
Related
I need the SQL for my classes, and I started training. I created microdb in AMAZON RDS, and downloaded MySQL Workbench. I successfully connected to my database, however, when I tried to add Figure 3.1 from the Silberschatz - Database system concepts, it failed to create tables with a foreign key with a famous error 1215. Here, the code:
create table department
(dept_name varchar (20),
building varchar (15),
budget numeric (12,2),
primary key (dept_name));
create table course
(course_id varchar (7),
title varchar (50),
dept_name varchar (20),
credits numeric (2,0),
primary key (course_id),
foreign key (dept_name) references department);
create table instructor
(ID varchar (5),
name varchar (20) not null,
dept_name varchar (20),
salary numeric (8,2),
primary key (ID),
foreign key (dept_name) references department);
create table section
(course_id varchar(8),
sec_id varchar (8),
semester varchar (6),
year numeric (4,0),
building varchar (15),
room_number varchar (7),
time_slot_id varchar (4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course);
create table teaches
(ID varchar (5),
course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year numeric (4,0),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section,
foreign key (ID) references instructor);
How should I handle this? The MySQL version is 5.7.2.
With regards, Ulvi
I have this simple code
create table transport (
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
primary key (CODE,TDATE,ID,PNAME) );
create table planes (
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
CONSTRAINT foreign key(NAME) references transport(PNAME));
But it doesn't let me do the foreign key thing as it says errno: 150 "Foreign key constraint is incorrectly formed
Any tips appreciated
Thanks
The use of composite primary keys in this case is not reasonable. If you want a foreign key reference, you need to reference all columns in the primary key. So, this version is simpler:
create table transport (
transportId int not null auto_increment primary key,
CODE varchar (10),
TDATE date,
ID integer,
PNAME varchar (10),
unique (CODE, TDATE, ID, PNAME)
);
create table planes (
planeId int not null auto_increment primary key,
NAME varchar (10),
NSEATS integer,
FSEATS integer,
ECSEATS integer,
primary key (NAME),
transportId int,
CONSTRAINT foreign key(transportId) references transport(transportId)
);
Rule is that : A Foreign key in one table points to a Primary Key in another table.
I have these tables but only managed to create
Insurance_Policy_Type
Historical_Era
Region
Reference
Organisation
Geographic_Area
The rest of them are giving me errors such as,
"Cant create table" on Loan_Item,
"Reference_Id dont exist" on Historical_Era_Reference
"error 1064 (4200)" on Loan
CREATE TABLE Insurance_Policy_Type (
Insurance_Policy_Type_Id INT (4),
Insurance_Policy_Type_Name VARCHAR (30),
PRIMARY KEY (Insurance_Policy_Type_Id));
CREATE TABLE Historical_Era (
Historical_Era_Id INT (4),
Historical_Era_Name VARCHAR (30),
PRIMARY KEY (Historical_Era_Id));
CREATE TABLE Reference (
Reference_Id INT (4),
Reference_Name VARCHAR (30),
Author VARCHAR (30),
PRIMARY KEY (Reference_Id));
CREATE TABLE Region (
Region_Id INT (4),
Region_Name VARCHAR (30),
PRIMARY KEY (Region_Id));
CREATE TABLE Organisation (
Organisation_Id INT (4),
Organisation_Name VARCHAR (30),
PRIMARY KEY (Organisation_Id));
CREATE TABLE Items (
Item_Id INT (4),
Item_Name VARCHAR (30),
Description VARCHAR (30),
Insurance_Policy_Type_Id INT (4),
Geo_Area_Id INT (4)
PRIMARY KEY (Item_Id),
FOREIGN KEY (Insurance_Policy_Type_Id) REFERENCES Insurance_Policy_Type(Insurance_Policy_Type_Id),
FOREIGN KEY (Geo_Area_Id) REFERENCES Geographic_Area(Geo_Area_Id));
CREATE TABLE GeoArea_HistEra (
Geo_Area_Id INT (4),
Historical_Era_Id INT (4),
FOREIGN KEY (Geo_Area_Id) REFERENCES Geographical_Area(Geo_Area_Id)),
FOREIGN KEY (Historical_Era_Id) REFERENCES Historical_Era(Historical_Era_Id));
CREATE TABLE Historical_Era_Reference (
References_Id INT (4),
Historical_Era_Id INT (4),
FOREIGN KEY (Reference_Id) REFERENCES Reference (Reference_Id),
FOREIGN KEY (Historical_Era_Id) REFERENCES Historical_Era (Historical_Era_Id));
CREATE TABLE Loan_Item (
Item_Id INT (4),
Loan_Id INT (4),
FOREIGN KEY (Item_Id) REFERENCES Items(Item_Id),
FOREIGN KEY (Loan_Id) REFERENCES Loan(Loan_Id));
CREATE TABLE Geographic_Area (
Geo_Area_Id INT (4),
Geo_Area_Name VARCHAR (30),
Region_Id INT (4),
PRIMARY KEY (Geo_Area_Id),
FOREIGN KEY (Region_Id) REFERENCES Region (Region_Id));
CREATE TABLE Loan (
Loan_Id INT (4),
Item_Id INT (4)
Organisation_Id INT(4)
Loan_Start DAY,
Loan_End DAY
FOREIGN KEY (Organisation_Id) REFERENCES Organisation (Organisation_Id),
FOREIGN KEY (Item_Id) REFERENCES Items (Item_Id));
Try to create the tables first and add the Foreign Keys later.
There are plenty of issues with the query above.
You are making a reference to a table which doesn't exist yet: "FOREIGN KEY (Geo_Area_Id) REFERENCES Geographic_Area(Geo_Area_Id));" First create the table and then update the foreign keys.
There are plenty of syntax errors - missing commas and double brackets. Instead of pointing them one by one, I'd suggest learning how to spot them, for example I suggest using an IDE. I personally test statements in MySQL Workbench.
The query will likely produce an error 150, for a number of reasons. Please see a very good summary on typical errors with primary keys as FK in the first answer here.
I am trying to create a query that shows the name and adress of all people who have incurred fines but do not have a driving license. (So do not show up in the driving license table) How would I go about including something who does not show up in a table?
Here are my tables
CREATE TABLE offences (
oOffenceID VARCHAR (30),
oDescription VARCHAR (200),
oMaximumFine INT (10),
CONSTRAINT o_pk
PRIMARY KEY (oOffenceID)
);
CREATE TABLE drivers (
dPersonID VARCHAR (30),
dLicenseNumber INT (20),
dLicenseRemoved ENUM('yes','no'),
dExpiryDate DATE,
CONSTRAINT d_pk
PRIMARY KEY (dLicenseNumber),
CONSTRAINT d_fk
FOREIGN KEY (dPersonId)
REFERENCES People (pPersonID)
);
CREATE TABLE people (
pPersonID VARCHAR (30),
pName VARCHAR (30),
pAddress VARCHAR (50),
pNIN VARCHAR (30),
CONSTRAINT p_pk
PRIMARY KEY (pPersonID)
);
CREATE TABLE vehicle (
vOwnerID VARCHAR (30),
vColour VARCHAR (30),
vModel VARCHAR (30),
vMake VARCHAR (30),
vVehicleID VARCHAR (30),
CONSTRAINT v_pk
PRIMARY KEY (vVehicleID),
CONSTRAINT v_fk
FOREIGN KEY (vOwnerID)
REFERENCES People (pPersonID)
);
CREATE TABLE fines (
fFineID INT (30) AUTO_INCREMENT,
fVehicleID VARCHAR (30),
fPersonID VARCHAR (30),
fTime DATE,
fAmount INT (10),
fOfficerStatement VARCHAR (500),
fOffenceID VARCHAR (30),
CONSTRAINT f_pk
PRIMARY KEY (fFineID),
CONSTRAINT f_fk
FOREIGN KEY (fVehicleID)
REFERENCES vehicle (vVehicleID),
CONSTRAINT f_fk1
FOREIGN KEY (fPersonID)
REFERENCES people (pPersonID),
CONSTRAINT f_fk2
FOREIGN KEY (fOffenceID)
REFERENCES offences (oOffenceID)
);
So far I have
SELECT DISTINCT pName, pAddress
FROM people, fines, drivers
WHERE
pPersonID <> dPersonID
AND
pPersonID = fPersonID
AND
fAmount > 0
but this doesn't remove all the necessary data.
Any help or guidance of the right tools to use would be much appreciated.
Use a LEFT JOIN and check for NULL values
example:
SELECT *
FROM
table_A
LEFT JOIN table_B
ON table_A.id = table_b.a_id
WHERE table_B.a_id IS NULL
I have a school assignment that that is giving me and my friends headaches... So this is my queries that i have executed, one at a time ofc...
CREATE DATABASE GamblingSociety;
USE GamblingSociety;
CREATE TABLE GamblingDen
(
Name VARCHAR (20) PRIMARY KEY,
Address VARCHAR (50),
Phone VARCHAR (10)
);
CREATE TABLE Room (
RoomNr INT,
GameCapaity INT,
GamblingDenName VARCHAR (20),
PRIMARY KEY (RoomNr, GamblingDenName),
FOREIGN KEY (GamblingDenName) REFERENCES GamblingDen (Name)
);
CREATE TABLE Employee (
SSN CHAR (11) PRIMARY KEY,
Name VARCHAR (20),
Address VARCHAR(50),
Salary INT,
isBoss BOOL,
GamblingDenName VARCHAR (20),
FOREIGN KEY (GamblingDenName) REFERENCES GamblingDen (Name)
);
CREATE TABLE GameType (
Name VARCHAR (20) PRIMARY KEY,
WinningProcentage FLOAT,
ResponsibleEmployee CHAR (11),
FOREIGN KEY (ResponsibleEmployee) REFERENCES Employee (SSN)
);
CREATE TABLE Supplier (
Name VARCHAR (20) PRIMARY KEY,
Address VARCHAR (50)
);
CREATE TABLE SupplierOfGameType (
SupplierName VARCHAR (20),
GameTypeName VARCHAR (20),
PRIMARY KEY(SupplierName, GameTypeName),
FOREIGN KEY (SupplierName) REFERENCES Supplier (Name),
FOREIGN KEY (GameTypeName) REFERENCES GameType (Name)
);
CREATE TABLE GamblingTable (
TableNr INT,
RoomNr INT,
GamblingDenName VARCHAR (20),
GameTypeName VARCHAR (20),
Comments VARCHAR (128),
PRIMARY KEY (RoomNr, TableNr, GamblingDenName, GameTypeName),
FOREIGN KEY (RoomNr, GamblingDenName) REFERENCES Room (RoomNr, GamblingDenName),
FOREIGN KEY (GameTypeName) REFERENCES GameType (Name)
);
CREATE TABLE Shylock (
Phone CHAR (10) PRIMARY KEY,
Name VARCHAR (20),
Contry CHAR (3)
);
CREATE TABLE Customer (
SSN CHAR (11) PRIMARY KEY,
Name VARCHAR (20),
Phone CHAR (10),
Address VARCHAR (50)
);
But this last query does not work:
USE GamblingSociety;
CREATE TABLE GamblingInstance (
StartTime DATETIME,
EndingTime DATETIME,
GamblingDenName VARCHAR (20),
TableNr INT,
GameTypeName VARCHAR (20),
Customer CHAR (11),
Shylock CHAR (10),
Debt INT,
DebtPayed BOOL,
DebtPayedDate DATETIME,
PRIMARY KEY (StartTime, GameTypeName, Customer),
FOREIGN KEY (GamblingDenName, TableNr, GameTypeName) REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),
FOREIGN KEY (Customer) REFERENCES Customer (SSN),
FOREIGN KEY (Shylock) REFERENCES Shylock (Phone)
);
It gives me this error:
"Error Code: 1005. Can't create table 'gamblingsociety.gamblinginstance' (errno: 150)"
if I replace this:
FOREIGN KEY (GamblingDenName, TableNr, GameTypeName) REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),
with this:
FOREIGN KEY (GameTypeName) REFERENCES GamblingTable (GameTypeName),
It works, so it's something with the GamblingDenName and TableNr.
And i have google it and found a lot of tips, but nothing yeat have worked...
So anyone with any idea?
Thanks in advance!
There is no key in GamblingTable that can satisfy this REFERENCE:
REFERENCES GamblingTable (GamblingDenName, TableNr, GameTypeName),
That is, there is no key in GamblingTable for which GamblingDenName, TableNr, GameTypeName are the only columns, or the first three columns.
There is a key that satisfies REFERENCES GamblingTable (GameTypeName), which is why that version works.
(I have to say that I've never actually seen FOREIGN KEYs being used to parent other FOREIGN KEYs, but I guess it works).