SQL-query error date - mysql

why does MySQL give me #1063 - Wrong column specification for column 'Date' error if I want to import this query? What I need to have in that row is Date with format: YYYY/MM/DD. Can someone help me and explain whats wrong and why this is wrong so I can learn :) Thank you!
CREATE TABLE Employee (
Employee_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Lastname VARCHAR (255) NOT NULL,
Firstname VARCHAR (255) NOT NULL,
Residence VARCHAR (255) NOT NULL,
Skilllevel TINYINT NOT NULL,
Salary DECIMAL NOT NULL
);
CREATE TABLE Direct_hours (
Date DATE AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
FOREIGN KEY Project_number INT NOT NULL REFERENCES Project (Project_number)
);
CREATE TABLE Indirect_hours (
Date DATE NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
Type VARCHAR (255) NOT NULL,
Number INT NOT NULL
);
CREATE TABLE Customer (
Customer_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Client_name VARCHAR (255) NOT NULL,
Residence VARCHAR (255) NOT NULL
);
CREATE TABLE Skilllevel (
Levels TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
Description VARCHAR (255) NOT NULL,
Hourly_rate TINYINT NOT NULL
);
CREATE TABLE Project (
Project_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
FOREIGN KEY Customer_number INT NOT NULL REFERENCES Customer (Customer_number),
Hourly_rate TINYINT NOT NULL
);

Related

Failed to add the foreign key constraint, MySQL

I have this error on PopSQL :
Error: Failed to add the foreign key constraint. Missing index for constraint 'etape__ibfk_1' in the referenced table 'etapexprojet_'
Error Code: ER_FK_NO_INDEX_PARENT
This is my code, i'm only creating tables and it has a problem in the "Etape_" part, i really, don't know why
I'm really, really new to SQL. Like... this is my first time doing this
--Client
CREATE TABLE IF NOT EXISTS jegere.client_ (
idClient INT PRIMARY KEY,
nomClient VARCHAR(45) NOT NULL,
adresse VARCHAR(45) NOT NULL UNIQUE,
telephone VARCHAR(45) NOT NULL UNIQUE,
adresseCourriel VARCHAR(45) NOT NULL UNIQUE
);
--Employés
CREATE TABLE IF NOT EXISTS jegere.Employe_ (
idEmploye INT PRIMARY KEY,
nomEmploye VARCHAR(45) NOT NULL,
adresse VARCHAR(45) NOT NULL UNIQUE,
telephone VARCHAR(45) NOT NULL UNIQUE,
adresseCourriel VARCHAR(45) NOT NULL UNIQUE
);
--Projet
CREATE TABLE IF NOT EXISTS jegere.Projet_ (
idProjet INT PRIMARY KEY,
idClient INT ,
nomProjet VARCHAR(45) NOT NULL ,
dateDebut DATE NOT NULL ,
dateFin DATE,
idResponsable INT NOT NULL ,
FOREIGN KEY (idClient ) REFERENCES jegere.Client_ (idClient ),
FOREIGN KEY (idResponsable ) REFERENCES jegere.Employe_ (idEmploye )
);
--RessourcesProjet
CREATE TABLE IF NOT EXISTS jegere.RessourcesProjet_ (
idProjet INT NOT NULL,
idEmploye INT NOT NULL,
nbrHeure INT NOT NULL,
PrixHeure FLOAT NOT NULL,
PRIMARY KEY(idProjet, idEmploye),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet),
FOREIGN KEY(idEmploye) REFERENCES employe_(idEmploye)
);
--Etape X Projet
CREATE TABLE IF NOT EXISTS jegere.EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet)
);
--Etapes
CREATE TABLE IF NOT EXISTS jegere.Etape_ (
idEtape INT,
nomEtape VARCHAR(45) NOT NULL,
Livrable VARCHAR(100) NOT NULL,
PRIMARY KEY(idEtape),
FOREIGN KEY(idEtape) REFERENCES etapexprojet_(idEtape)
);
The referenced column in a foreign key needs to be indexed, so you need to add an index on the idEtape column in etapexprojet:
--Etape X Projet
CREATE TABLE IF NOT EXISTS jegere.EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
INDEX (idEtape),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet)
);
Having it as part of the primary key isn't sufficient. A prefix of an index is also an index, but idEtape is a later part of the primary key index, so it's not indexed by itself.
Another solution is to change the order of the primary key:
PRIMARY KEY(idEtape, idProject),
Your last table etape references only the column idetape, but all referenced columns need an index , primary key or UNIQUE constraint.
so add a KEY to your EtapexProjet_ for the column idetape, and you can run the code
CREATE TABLE IF NOT EXISTS EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
KEY(idEtape),
FOREIGN KEY(idProjet) REFERENCES Projet_(idProjet)
);
see https://dbfiddle.uk/z9QFoH0c

MySQL - Nonsense Syntax Errors

I'm new to programming, and cannot understand the syntax errors that I'm getting.
CREATE SCHEMA IF NOT EXISTS donutsrus;
CREATE TABLE IF NOT EXISTS DONUT (
Donut_ID INT NOT NULL, UNIQUE
Donut_Name VARCHAR (20) NOT NULL, UNIQUE
Description VARCHAR (20) NOT NULL, UNIQUE
Unit_Price NUMERIC (4,2) NOT NULL
PRIMARY KEY (Donut_ID));
CREATE TABLE IF NOT EXISTS SALES_ORDER (
Sales_Order_ID INT NOT NULL, UNIQUE
Sales_Order_Date DATE NOT NULL
Sales_Order_Notes VARCHAR (100)
Customer_ID INT NOT NULL, UNIQUE
PRIMARY KEY (Sales_Order_ID)
FOREIGN KEY (Customer_ID));
CREATE TABLE IF NOT EXISTS CUSTOMER (
Customer_ID INT NOT NULL, UNIQUE
Last_Name VARCHAR (20) NOT NULL
First_Name VARCHAR (20) NOT NULL
Address VARCHAR (50) NOT NULL
Apt VARCHAR (5)
City VARCHAR (20) NOT NULL
State CHAR (2) NOT NULL
Zip NUMBER (5) NOT NULL
Home_Phone NUMBER (10)
Mobile_Phone NUMBER (10)
Other_Phone NUMBER (10)
PRIMARY KEY (Customer_ID));
CREATE TABLE IF NOT EXISTS ORDER_LINE_ITEM (
Donut_ID INT NOT NULL, UNIQUE
Sales_Order_ID INT NOT NULL, UNIQUE
Quantity INT NOT NULL
PRIMARY KEY (Donut_ID, Sales_Order_ID)
FOREIGN KEY (Donut_ID, Sales_Order_ID));
On the Donut table, I get Syntax error: extraneous input found - expected 'opening parenthesis', at the VARCHAR after Donut_Name. At the closing paren after the succeeding 20, I get Syntax error: extraneous input found - expected 'closing parenthesis'.
On the Order Line Item table I get, Syntax Error: 'INT' (int) is not valid input at this position, after the Sales_Order_ID. I also don't understand why it's okay on other lines of code, but not this one.
All the commas before UNIQUE are wrong. Commas are used to separate the columns, not to separate options for a specific column.
CREATE TABLE IF NOT EXISTS DONUT (
Donut_ID INT NOT NULL UNIQUE,
Donut_Name VARCHAR (20) NOT NULL UNIQUE,
Description VARCHAR (20) NOT NULL UNIQUE,
Unit_Price NUMERIC (4,2) NOT NULL,
PRIMARY KEY (Donut_ID));
CREATE TABLE IF NOT EXISTS CUSTOMER (
Customer_ID INT NOT NULL UNIQUE,
Last_Name VARCHAR (20) NOT NULL,
First_Name VARCHAR (20) NOT NULL,
Address VARCHAR (50) NOT NULL,
Apt VARCHAR (5),
City VARCHAR (20) NOT NULL,
State CHAR (2) NOT NULL,
Zip NUMBER (5) NOT NULL,
Home_Phone NUMBER (10),
Mobile_Phone NUMBER (10),
Other_Phone NUMBER (10),
PRIMARY KEY (Customer_ID));
CREATE TABLE IF NOT EXISTS SALES_ORDER (
Sales_Order_ID INT NOT NULL UNIQUE,
Sales_Order_Date DATE NOT NULL,
Sales_Order_Notes VARCHAR (100),
Customer_ID INT NOT NULL,
PRIMARY KEY (Sales_Order_ID),
FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER (Customer_ID));
CREATE TABLE IF NOT EXISTS ORDER_LINE_ITEM (
Donut_ID INT NOT NULL,
Sales_Order_ID INT NOT NULL,
Quantity INT NOT NULL,
PRIMARY KEY (Donut_ID, Sales_Order_ID),
FOREIGN KEY (Donut_ID) REFERENCES DONUT (Donut_ID),
FOREIGN KEY (Sales_Order_ID) REFERENCES SALES_ORDER (Sales_Order_ID));
BTW, the UNIQUE option is not necessary for primary keys, they're automatically made unique. And in ORDER_LINE_ITEM, if the primary key is (Donut_ID, Sales_Order_ID) you probably don't want to make each of those columns unique by themselves. This prevents the same donut from being in multiple orders, or an order having more than one donut type. Similarly, Customer_ID should not be unique in SALES_ORDER; a customer can obviously have more than one order.
Your FOREIGN KEY lines were missing the references of the foreign keys. And the tables with the foreign keys need to be created after the tables they reference, so I moved SALES_ORDER down to after CUSTOMER.

ERROR 1005 (HY000): Can't create table 'project2.fulltime' (errno: 150)

I know you all have answered this question lots of times. Please help if you have the time. It can't create Full Time table or Part Time table. I am getting an error HY000. Tired to fix it reading through the forms still getting the error. thank you for your time.
DROP DATABASE project2;
CREATE DATABASE project2;
Use project2;
drop table if exists Customer;
drop table if exists Employee;
drop table if exists Orders;
drop table if exists BookOrder;
drop table if exists FullTime;
drop table if exists PartTime;
CREATE TABLE Customer (
CustomerID int PRIMARY KEY ,
FirstName varchar (50) NOT NULL ,
LastName varchar (50) NOT NULL ,
Address varchar (60) NULL ,
City varchar (25) NULL ,
State varchar (15) NULL ,
Phone varchar (16) NULL ,
Email varchar(50) NULL
);
Insert Into Customer Values(1,'Jhon','Peterson','3265 4th Street','Los Angeles','CA','5625687854','Jhon.Peterson#yahoo.com');
Insert Into Customer Values(2,'Peter','Paker','9587 Church Ave.','Compton','CA','3235687452','Peter.Paker#marvel.com');
Insert Into Customer Values(3,'Yin','May','2354 Bombardier Ave.','New Rochelle','NY','6088565216','Yin.May#hotmail.com');
Insert Into Customer Values(4,'Erick','Ruiz','1234 Franklin Blvd.','Columbus','OH','9065682561','Erick.Ruiz#gmai.com');
Insert Into Customer Values(5,'Alex','Bastidas','5698 Luders Street','Paris','UK','8985354587','Alex.Bastidas#gmail.com');
CREATE TABLE Employee(
EmployeeID int PRIMARY KEY,
FirstName varchar (50) NOT NULL ,
LastName varchar (50) NOT NULL ,
Address varchar (60) NULL ,
City varchar (25) NULL ,
State varchar (15) NULL ,
Phone varchar (16) NULL,
SSN varchar (16) NOT NULL
);
Insert Into Employee Values(32,'Juan','Ortiz','5748 Pine Street','Los Angeles','CA','3232587458','608758955');
Insert Into Employee Values(45,'Mark','Marks','5148 Hatteras','Long Beach','CA','2135487632','405734655');
Insert Into Employee Values(50,'Van','Diesel','4784 Sixth Street','Van Nuys','NC','6058569265','702457822');
Insert Into Employee Values(16,'June','Franks','98456 Home Ave.','Ontario','NJ','5624875632','608159555');
Insert Into Employee Values(22,'Adrianna','Ornals','498756 Bronxs Street','Brooklyn','NY','1234567888','123456789');
CREATE TABLE Orders (
OrderID int PRIMARY KEY ,
CustomerID int (50) NULL ,
EmployeeID int (50) NOT NULL
);
Insert Into Orders Values(10,5,32);
Insert Into Orders Values(14,3,45);
Insert Into Orders Values(11,2,16);
Insert Into Orders Values(13,1,50);
Insert Into Orders Values(12,4,22);
CREATE TABLE BookOrder(
BookID int (50) NOT NULL,
OrderID int (50) NOT NULL ,
OrderQuantity int (50) NOT NULL,
CONSTRAINT BookOrderPK PRIMARY KEY(BookID,OrderID)
);
Insert Into BookOrder Values(1,14,7);
Insert Into BookOrder Values(20,10,10);
Insert Into BookOrder Values(34,11,25);
Insert Into BookOrder Values(11,12,31);
Insert Into BookOrder Values(21,13,25);
CREATE TABLE Book(
BookID int PRIMARY KEY,
StockQuantity int (50) NOT NULL ,
BookTitle varchar (50) NOT NULL ,
BookAuthor varchar (50) NOT NULL,
BookPrice decimal(4,2) NOT NULL
);
Insert Into Book Values(22,10,'Literature', 'Mark Patternson', 60.99);
Insert Into Book Values(64,25,'Calculus', 'Gilbert Strang', 187.99);
Insert Into Book Values(87,30,'Java', 'Daniel Liang', 150.99);
Insert Into Book Values(90,40,'Philosophy', 'Marcus Aurelius', 90.99);
Insert Into Book Values(77,5,'Social Engineering', 'Christopher Hadnagy', 100.99);
CREATE TABLE FullTime(
EmployeeID int PRIMARY KEY,
Salary varchar(50) NOT NULL,
SSN varchar (16) NOT NULL,
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID),
FOREIGN KEY (SSN) REFERENCES Employee(SSN)
);
Insert Into FullTime Values(22,'62,990','123456789');
Insert Into FullTime Values(50,'51,730','702457822');
Insert Into FullTime Values(45,'46,990','405734655');
CREATE TABLE PartTime(
EmployeeID int PRIMARY KEY,
HourlyRate decimal(4,2) NOT NULL,
TotalHours int (40) NOT NULL,
SSN varchar (16) NOT NULL,
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID),
FOREIGN KEY (SSN) REFERENCES Employee(SSN)
);
Insert Into PartTime Values(32,32.00,35,'608758955');
Insert Into PartTime Values(16,40.00,38,'608159555');
You need to make SSN in employee table a unique key, which is one in real life any way, so that it can be referenced as a foreign key:
CREATE TABLE Employee(
EmployeeID int PRIMARY KEY,
FirstName varchar (50) NOT NULL ,
LastName varchar (50) NOT NULL ,
Address varchar (60) NULL ,
City varchar (25) NULL ,
State varchar (15) NULL ,
Phone varchar (16) NULL,
SSN varchar (16) NOT NULL,
unique(SSN)
);

Having trouble in Online Movie ticketing database design

#J2D8T i have done few changes in my code please tell if its correct also is it properly normalized? thanks in advance.
use [fadi1]
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Movie (
M_id int primary key not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null,
Actors varchar (100) not null,
);
create table Venue(
V_id int primary key not null,
V_Name varchar(25) not null,
Maxcapacity int not null,
);
create table Shows (
showid int primary key IDENTITY(1,1) not null,
V_id int not null,
M_id int not null ,
date_time datetime not null unique,
remaining_tickets int not null,
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
);
create table Tickets(
showid int not null,
Seat_no int not null, -- (maxcap-remaining tickets)+1 from capacity table of given pk of that table
T_id int primary key IDENTITY(1,1) not null, --(p.k)
price int not null,
foreign key (showid) references Shows(showid)
);
create table Booking (
showid int not null,
T_id int not null unique,
Cnic varchar(17) not null,
booking_time datetime not null,
foreign key (showid) references Shows(showid),
foreign key (T_id) references Tickets(T_id),
foreign key (Cnic) references Customer(Cnic),
constraint pk_IDBooking primary key(T_id,showid),
);
please inform me as soon as possible because i am not quite far from the deadline :D
At a minimum, there is no password data type in SQL Server. Since you have provided no details about how you're storing data, I converted it to varchar(50).
SQLFiddle
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar(50) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30) ,
)
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar(50) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30) ,
)
create table Movie (
M_id int not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null,
primary key (M_id)
);
create table Actors(
actor_id varchar(25) primary key not null ,
actor varchar(30)not null,
M_id int not null,
Act_role varchar (10)not null,
amountspent int ,
foreign key (M_id) REFERENCES Movie(M_id)
);
create table Venue(
V_id int primary key not null,
M_id int not null,--F.k
V_Name varchar(25)not null
);
create table Capacity(
M_id int not null,
v_id int not null,
date_time varchar (15) not null,
remaining_tickets int not null,
max_capacity int not null,
primary key (M_id, v_id ,date_time )
);
create table Booking (
v_id int not null, --key
T_id int not null,
M_id int not null,--key
Cnic varchar(30) primary key not null,--(p.k))
date_time varchar (15) not null, -- (f.k )
);
create table Tickets(
Seat_no int primary key not null, -- (p.k))
T_id int not null,
M_id int not null, --(f.k)
price int not null,
);
I edited the answer to include only the code with some comments hope this solves your problem.
Edit from your original post:
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword password not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null, // changed to varchar
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Movie (
M_id int primary key not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null
);
create table Actors(
actor_id varchar(25) primary key not null ,
actor varchar(30)not null,
M_id int not null,
Act_role varchar (10)not null,
amountspent int ,
foreign key (M_id) REFERENCES Movie(M_id)
);
create table Venue(
V_id int primary key not null,
M_id int not null,--F.k
V_Name varchar(25) not null,
foreign key (M_id) references Movie(M_id)
);
create table Capacity(
v_id int not null, // FK
M_id int not null unique, //FK
date_time varchar (15) not null unique,
remaining_tickets int not null,
max_capacity int not null,
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
constraint pk_IDCapacity PRIMARY KEY (M_id,v_id,date_time) // this will create composite key
);
create table Tickets(
Seat_no int primary key not null, -- (p.k))
T_id int not null,
M_id int not null, --(f.k)
price int not null,
foreign key (M_id) references Capacity(M_id)
);
create table Booking (
v_id int not null, //FK
T_id int not null, //FK
M_id int not null,//FK
Cnic varchar(30) primary key not null // Will this stil be neccessary
date_time varchar (15) not null, //FK
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
foreign key (date_time) references Capacity(date_time),
foreign key (T_id) references Tickets(T_id),
constraint pk_IDBooking primary key(v_id,M_id,T_id,date_time) //Can be in any order the PK's
);
Could probably be more compacted with normalization but it should work based on the code you have provided.

foreign key reference syntax error

I am having trouble creating members table with the following code. check the manual that corresponds to your MySQL server version for the right syntax to use near 'schoolID int NOT NULL FOREIGN KEY REFERENCES schools(schoolID),' at line 1
What is wrong with the syntax?
Thanks!
CREATE TABLE schools (
schoolID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
parentID int NOT NULL DEFAULT 0,
schoolname VARCHAR(199) NOT NULL,
active int NOT NULL,
dateENTERED datetime NOT NULL
);
CREATE TABLE members
(
memberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
schoolID int NOT NULL FOREIGN KEY REFERENCES schools(schoolID),
active int NOT NULL,
dateENTERED datetime NOT NULL
);
CREATE TABLE members
(
memberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
schoolID int NOT NULL ,
active int NOT NULL,
dateENTERED datetime NOT NULL,
CONSTRAINT member_FK FOREIGN KEY (schoolID) REFERENCES schools(schoolID)
);
SQLFiddle Demo
Purpose of Constraint Naming
you can also do it this way,
CREATE TABLE members
(
memberID int NOT NULL AUTO_INCREMENT,
schoolID int NOT NULL ,
active int NOT NULL,
dateENTERED datetime NOT NULL,
CONSTRAINT member_PK PRIMARY KEY (memberID),
CONSTRAINT member_FK FOREIGN KEY (schoolID) REFERENCES schools(schoolID)
);
SQLFiddle Demo