Beginner - How to place info in tables - CREATE TABLE - mysql

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

Related

MYSQL Foreign key incorrectly formed

Have an error where my first foreign key constraint is incorrectly formed. I'm waiting to hear back from my lecturer if she can spot the issue but thought I'd ask here
My SQL query is as follows
CREATE DATABASE rugby;
USE rugby;
CREATE TABLE address(
address_ID INT AUTO_INCREMENT NOT NULL,
address_name VARCHAR(50) NOT NULL,
PRIMARY KEY (address_ID)
) ENGINE=INNODB;
CREATE TABLE team(
team_ID INT AUTO_INCREMENT NOT NULL,
team_Name VARCHAR(25) NOT NULL,
team_Year INT NOT NULL,
PRIMARY KEY(team_ID)
) ENGINE=INNODB;
CREATE TABLE person(
person_ID INT AUTO_INCREMENT NOT NULL,
name VARCHAR(30) NOT NULL,
phone INT NOT NULL,
address_ID INT NOT NULL,
email VARCHAR(50),
photo BLOB,
PRIMARY KEY(person_ID),
FOREIGN KEY(address_ID) REFERENCES address(address_ID) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=INNODB;
I have followed how we were taught but can't spot the issue.
Any help appreciated
In table person you have defined address_ID as NOT NULL, but
when you define the FOREIGN KEY you set:
ON DELETE SET NULL
which contradicts the NOT NULL definition.

Trying to run a query that will show us the percentages of outbound leads vs. inbound leads

So far our code is below but it is giving us an error
Select LeadSource, (count(‘Inbound’) from Lead_T * 100 / select count (LeadSource) from Leads_T)) as percent
Where LeadID in( select stage
From opportunities_T
Where Opportunities_T.stage = ‘7-Closed/Won’)
Table 1:
create table Opportunities_T
(Executive varchar(50) not null,
AccountName varchar(150) not null,
Stage varchar(50) not null,
OpAmount int not null,
CloseDate date not null,
AccountStatus varchar(15) not null,
LeadID int not null,
constraint Opportunities_PK primary key (LeadID, AccountName),
constraint Opportunities_FK1 foreign key (LeadID) references Leads_T(LeadID),
constraint Opportunities_FK2 foreign key (AccountName) references Account_T(AccountName));
Table 2:
create table Leads_T
(Contact varchar(25),
Company varchar(100),
Title varchar(150),
LeadSource varchar(30),
BDR varchar(50),
LeadID int,
constraint Leads_PK primary key (LeadID));

mysql - cannot add foreign key constraint when creating a table

I'm new to sql.
I get this error when I try to create foreign keys:
cannot add foreign key constraint
when I'm trying to create the ORDERS table. here is my code:
drop database if exists Company;
create database Company;
use Company;
create table WORKERS(w_id varchar(4), w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4), customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
create table WAREHOUSE(m_id int unsigned primary key, describes varchar(20) not null, fl char(1));
create table ITEM(p_id int(4) unsigned primary key, p_name varchar(15) not null, p_price float not null);
create table INVENTORY(in_id int auto_increment primary key, m_id int unsigned not null, p_id int(4) unsigned not null, amount int not null,
foreign key (m_id) references WAREHOUSE(m_id),
foreign key (p_id) references ITEM(p_id)
);
create table ORDERS(o_id int auto_increment primary key, customer_id varchar(4),
p_id int(4) unsigned, w_id varchar(4), amount int unsigned not null,
date_of_order date not null,
foreign key (p_id) references ITEM(p_id),
foreign key (w_id) references WORKERS(w_id),
foreign key (customer_id) references CUSTOMERS(customer_id)
);
I put the ORDERS (where I have the problem) on different lines to make it easier to you to read.
I search here for an answer, but didn't found anything specific that answer my question.
anyone got any idea what the problem is? thank you!
The problem is that you are trying to create a FOREIGN KEY relationship to a field in the WORKERS and CUSTOMERS tables that aren't set up as a PRIMARY KEY.
The FOREIGN KEY needs to be pointing to a PRIMARY KEY. Change your create script to the following:
create table WORKERS(w_id varchar(4) primary key, w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4) primary key, customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
A word of caution, though. I would recommend not using VARCHAR (4) as a PRIMARY KEY field. I would recommend using an INT AUTO_INCREMENT instead.
The column of the referenced table should be declared as primary key to assign a foreign key. For your orders table, WORKERS(w_id) and CUSTOMERS(customer_id) are not declared as primary key, hence you get the error.
Modified Statements:
drop database if exists Company;
create database Company;
use Company;
create table WORKERS(w_id varchar(4) primary key, w_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique);
create table CUSTOMERS(customer_id varchar(4) primary key, customer_name varchar(20) not null, telephone varchar(12), e_mail varchar(35) unique, credit bool);
create table WAREHOUSE(m_id int unsigned primary key, describes varchar(20) not null, fl char(1));
create table ITEM(p_id int(4) unsigned primary key, p_name varchar(15) not null, p_price float not null);
create table INVENTORY(in_id int auto_increment primary key, m_id int unsigned not null, p_id int(4) unsigned not null, amount int not null,
foreign key (m_id) references WAREHOUSE(m_id),
foreign key (p_id) references ITEM(p_id)
);
create table ORDERS(o_id int auto_increment primary key, customer_id varchar(4),
p_id int(4) unsigned, w_id varchar(4), amount int unsigned not null,
date_of_order date not null,
foreign key (p_id) references ITEM(p_id),
foreign key (w_id) references WORKERS(w_id),
foreign key (customer_id) references CUSTOMERS(customer_id)
);
SQLFiddle Demo

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.

How to create table in mysql database with foreign key which is primary key of another table?

CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id),
);
This is my first table(employee_login)and I want e_id as a foreign key in my next table (login) below
CREATE TABLE login(
login_id int auto_increment,
username varchar(20) not null,
password varchar(20) not null,
primary key (login_id),
e_id references employee_detail(e_id)
);
You can do it like follows :
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
I hope you understand the create table code. owner in the shirt table is the foreign key referencing id from the person table
You have several mistakes. You were missing e_id column in your Login Table, you can't add a foreign key without adding employee_detail's primary key column which in this case is e_id. Also you can't use password as a column name since it is a preestablished query you'll need to use something else like "pass".
CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id));
CREATE TABLE login(
login_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
pass VARCHAR(20) NOT NULL,
e_id INT,
FOREIGN KEY (e_id) REFERENCES employee_detail(e_id)
);