wish to use empID from `users` table to `leave` table - mysql

i am relatively new in phpmysql.i need some help...
i have two table namely users and leave.In users table i have following column
empID(int)
username(varchar)
password(varchar)
mac(varchar)
ip(varchar)
and in leave table i have following column
lid(int)
empname(varchar)
username(varchar)
nod(int)
sdate(date)
edate(date)
reason(varchar)
action(varchar)
Now i want to use empID in leave table.how can i use it.i have to say that i already apply a code but its not working.Its giving the following error..
1072 - Key column 'empID' doesn't exist in table
and the code
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
so whats wrong...please help me..

create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
should be
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID int, // here you need one like this
empname VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
nod INT NOT NULL,
sdate DATE,
edate DATE,
reason VARCHAR(255) NOT NULL,
PRIMARY KEY(lid),
FOREIGN KEY(empID) REFERENCES users(empID)
);
you are missing a empID column.

Your SQL to create the leave table includes the correct bit to create the foreign key relationship:
FOREIGN KEY(empID) REFERENCES users(empID)
but that only creates the relationship between the tables, based on the empID columns. It doesn't create the empID column for you! You also need a line above, to define the empID column in the leave table, something like this:
create table `leave`(
lid INT NOT NULL AUTO_INCREMENT,
empID INT NOT NULL,
empname ...
... more lines ,
FOREIGN KEY(empID) REFERENCES users(empID)

Related

Incorrect syntax near the keyword 'CONSTRAINT'

I am getting this error message ...
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'CONSTRAINT'.
I've done my research and I'm still stuck on completing this script.
Here`s what I entered.
DROP TABLE SEMESTER;
DROP TABLE CLASS;
DROP TABLE STUDENT;
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null);
CREATE TABLE CLASS (
title CHAR(40) not null PRIMARY KEY,
instructor CHAR(40) not null );
CREATE TABLE SEMESTER (
year int not null );
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),
CONSTRAINT pk_class PRIMARY KEY (title, instructor),
CONSTRAINT pk_semester PRIMARY KEY (semid, year));
I think those CONSTRAINT statements need to be inside the CREATE TABLE statements. For example, the STUDENT table should look like this:
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null,
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),
);
Otherwise, how could the RDBMS know to which table each constraint actually belongs?
DROP TABLE SEMESTER;
DROP TABLE CLASS;
DROP TABLE STUDENT;
CREATE TABLE STUDENT (
stuid int not null,
stulname CHAR(40) not null,
stufname CHAR(40) not null,
stugender CHAR(1) not null,
stubirthdate DATE not null
CONSTRAINT pk_student PRIMARY KEY (stuid, stulname, stufname, stugender, stubirthdate),);
CREATE TABLE CLASS (
title CHAR(40) not null ,
instructor CHAR(40) not null ,
CONSTRAINT pk_class PRIMARY KEY (title, instructor));
CREATE TABLE SEMESTER (
year int not null ,
semid int not null
CONSTRAINT pk_semester PRIMARY KEY (semid, year));;
AS per my understanding
Here constraint must be create inside the table for student
and
second table u are creating 2 times primary key on class(title)
and
coming to third table semid itself not present in semester table

Beginner - How to place info in tables - CREATE TABLE

I have to make a database and i'm already stuck with something. I want to make a database where i will have 5 tables. User, complaint, missing, wanted and stolen objects.
So I want to make a database where a user puts his info, then file a complaint. The user has the choice about 3 complaint subjects: missing, wanted or stolen object. So after the complaint, it should be placed in the right table.
I'm not sure on how to place the right info in the right table, I'm new with mySQL..
Here is my database for the moment:
CREATE TABLE User (
user_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15) NOT NULL,
sexe VARCHAR(1) NOT NULL,
age INT(3) NOT NULL,
birthplace VARCHAR(50),
phoneNumber VARCHAR(20) NOT NULL,
email VARCHAR(50),
PRIMARY KEY (user_id)
);
CREATE TABLE complaint (
complaint_id INT NOT NULL AUTO_INCREMENT,
user INT NOT NULL,
complaint_sort VARCHAR(16),
title VARCHAR(150),
name VARCHAR(15),
date_complaint,
place VARCHAR(50) NOT NULL,
description VARCHAR(2000) NOT NULL,
PRIMARY KEY (complaint_id),
FOREIGN KEY (user_id)
REFERENCES Gebruiker (gebruiker_id)
);
CREATE TABLE missing (
missing_id INT NOT NULL AUTO_INCREMENT,
complaint_id INT,
PRIMARY KEY (missing_id),
FOREIGN KEY (complaint_id)
REFERENCES Complaint (complaint_id)
);
CREATE TABLE wanted (
wanted_id INT NOT NULL AUTO_INCREMENT,
complaint_id INT,
PRIMARY KEY (wanted_id),
FOREIGN KEY (complaint_id)
REFERENCES Complaint (complaint_id)
);
CREATE TABLE Stolen_objects (
Stolen_objects_id INT NOT NULL AUTO_INCREMENT,
complaint_id INT,
PRIMARY KEY (stolen_objects_id),
FOREIGN KEY (complaint_id)
REFERENCES Complaint (complaint_id)
);
There is no need to create three tables for complaint types - missing, wanted, stolen_objects.
Instead of creating three tables just add a flag in complaint table for complaintType with ENUM as data type with values like missing, wanted, stolenObjects.
Try this:
CREATE TABLE USER (
user_id INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(15) NOT NULL,
sexe VARCHAR(1) NOT NULL,
age INT(3) NOT NULL,
birthplace VARCHAR(50),
phoneNumber VARCHAR(20) NOT NULL,
email VARCHAR(50),
PRIMARY KEY (user_id)
);
CREATE TABLE complaint (
complaint_id INT NOT NULL AUTO_INCREMENT,
USER INT NOT NULL,
complaintType ENUM('Missing','Wanted','stolenObjects') NOT NULL,
complaint_sort VARCHAR(16),
title VARCHAR(150),
NAME VARCHAR(15),
date_complaint DATETIME,
place VARCHAR(50) NOT NULL,
description VARCHAR(2000) NOT NULL,
PRIMARY KEY (complaint_id),
FOREIGN KEY (user_id) REFERENCES USER (user_id)
);

How add a customer to an order when getting the customer details from a form

I am having trouble adding a customer and their order to an order table once they have checked out.
Here is my SQL for creating the four tables I am using:
CREATE TABLE IF NOT EXISTS Product(
ID int NOT NULL AUTO_INCREMENT,
Name varchar(255) NOT NULL,
Description text(65535) NOT NULL,
Quantity int NOT NULL,
Photo varchar(255),
Price float NOT NULL,
Category varchar(50),
PRIMARY KEY (ID)
) ENGINE=innoDB;
CREATE TABLE IF NOT EXISTS Customer(
ID int NOT NULL AUTO_INCREMENT,
FirstName varchar(255) NOT NULL,
LastName varchar(255) NOT NULL,
Email varchar(255) NOT NULL,
PhoneNumber varchar(11) NOT NULL,
Address varchar(50),
Town varchar(50),
County varchar(50),
PostCode varchar(50),
PRIMARY KEY (ID)
) ENGINE=innoDB;
CREATE TABLE IF NOT EXISTS OrderTable(
ID int NOT NULL AUTO_INCREMENT,
Date date NOT NULL,
PRIMARY KEY (ID),
TotalPrice float NOT NULL,
Customer_ID int NOT NULL,
CONSTRAINT fk_Order_1
FOREIGN KEY (Customer_ID)
REFERENCES coursework_db.Customer (ID)
) ENGINE=innoDB;
CREATE TABLE IF NOT EXISTS OrderItem(
ID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ID),
Product_ID int NOT NULL,
Order_ID int NOT NULL,
Quantity int NOT NULL,
TotalPrice float NOT NULL,
CONSTRAINT fk_OrderItem_1
FOREIGN KEY (Product_ID)
REFERENCES coursework_db.Product(ID),
CONSTRAINT fk_OrderItem_2
FOREIGN KEY (Order_ID)
REFERENCES coursework_db.OrderTable(ID)
) ENGINE=innoDB;
The problem I am having is how to select the customer from the table once they have been added to the database to use as a foreign key in the OrderTable table.
At the moment I have the details of the customer stored in local storage which can easily be accessed, but once the customer is added to the database they will get an ID. This is the only way I could think to select a unique customer.
Insert the customer details first. Then get the ID of the newly inserted customer and use it while inserting the order details!
You could use the LAST_INSERT_ID() after you insert the user details to the db to get the ID of the customer.
Or if you are using PHP, then:
if you're using PDO, use PDO::lastInsertId
if you're using Mysqli, use mysqli::$insert_id
Hope this helps.

SQL error 1215(HY000) cannot add foreign key constraint.

I looked through other questions on this topic, and the general consensus was to check my data type consistency. I am trying to create the table 'reviews' that has SSN and ProjectID as foreign keys, referencing SSN from Table Reviewer and ProjectID from Table Project
CREATE TABLE Reviews(
ProjectID VARCHAR(10) NOT NULL,
SSN INTEGER NOT NULL CHECK
(SSN>100000000 AND SSN<999999999),
ReviewerRole VARCHAR(50) NOT NULL,
FOREIGN KEY(SSN)REFERENCES Reviewer(SSN),
FOREIGN KEY(ProjectID)REFERENCES Project(ProjectID),
PRIMARY KEY(SSN,ProjectID)
);
The following are the create table statements for 'Project' and 'Reviewer'
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
CREATE TABLE Project(
ProjectID VARCHAR(10) NOT NULL,
Title VARCHAR(50),
Archived DATE NOT NULL,
ProjectStatus VARCHAR(50) NOT NULL,
PRIMARY KEY(ProjectID)
);
Project and Reviewer created without issue. My create Reviews query works if I remove the FK declaration for SSN and leave the FK declaration for ProjectID, but not the other way around.
What is the reason for the error? Is there another constraint I need to add?
You forgot to add a primary key to the SSN column in the Reviewer table. The query below builds correctly in sqlfiddle:
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
PRIMARY KEY(SSN)
);
CREATE TABLE Project(
ProjectID VARCHAR(10) NOT NULL,
Title VARCHAR(50),
Archived DATE NOT NULL,
ProjectStatus VARCHAR(50) NOT NULL,
PRIMARY KEY(ProjectID)
);
CREATE TABLE Reviews(
ProjectID VARCHAR(10) NOT NULL,
SSN INTEGER NOT NULL CHECK
(SSN>100000000 AND SSN<999999999),
ReviewerRole VARCHAR(50) NOT NULL,
FOREIGN KEY(SSN)REFERENCES Reviewer(SSN),
FOREIGN KEY(ProjectID)REFERENCES Project(ProjectID),
PRIMARY KEY(SSN,ProjectID)
);
A foreign key can only reference to a Primary Key column in another table, you need to make SNN a primary key column.
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL primary key CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
The referenced column for a foreign key column, needs to be Primary key OR have a unique constraint so your Reviewer table need to be:
CREATE TABLE Reviewer(
SSN INTEGER PRIMARY KEY CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
or
CREATE TABLE Reviewer(
SSN INTEGER NOT NULL CHECK (SSN>100000000 AND SSN<999999999),
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
UNIQUE(SSN)
);
Also as far as I know, MySQL Ignores Check Constraints, so you can remove them.

Alter Table Error:150

I cant alter the table to make add a foreign key. Error: 150 comes up. Can't figure out what is wrong. Please assist
CREATE TABLE Staff (staffNo varchar(10) NOT NULL, Fname varchar(50),
Lname varchar(50), Staff_Adress varchar(100), Salary numeric(65),
JobPosition varchar(15), Sex varchar(1), DateOfBirth date, NIN varchar(25),OffceNo varchar(10),
PRIMARY KEY (staffNo),
CHECK (Sex IN ('M', 'F')));
CREATE TABLE Office (OfficeNo INT(10) unsigned NOT NULL, Address varchar(50) NOT NULL, City varchar (25) NOT NULL,
PhoneNo varchar(10) NOT NULL, ManagerNo varchar(10),
PRIMARY KEY (OfficeNo),
FOREIGN KEY (ManagerNo) references Staff(staffNo));
alter table Staff
add foreign key (OffceNo) references Office(OfficeNo) on delete set NULL;
Fix these issues:
Tables have to be using InnoDB
Columns have to have exact same data types
your problem is here :
alter table Staff
add foreign key (OffceNo) references Office(OfficeNo) on delete set NULL;
you have to change this in Staff table
OffceNo varchar(10)
to
OffceNo INT(10) unsigned
Or this in Office table.
OfficeNo INT(10) unsigned NOT NULL
to
OfficeNo varchar(10) NOT NULL
for they will be same structure and same data types to use foreign keys.