foreign key reference syntax error - mysql

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

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

Nested query in a view is giving me CHECK OPTION on non-updatable view

CREATE VIEW Res_borrow
AS
SELECT *
FROM borrowing
WHERE mem_id IN (
SELECT mpd.mem_id
FROM (
SELECT mem_id, COUNT(*) AS no_curr_bks
FROM borrowing
WHERE borrowing.ret_date IS NULL
GROUP BY borrowing.mem_id
)Y
RIGHT JOIN mpd ON y.mem_id = mpd.mem_id
WHERE y.no_curr_bks < mpd.nob OR y.no_curr_bks IS NULL
)
WITH CHECK OPTION;
I tried to create such a view but i am getting error
Here is the schema of db
CREATE TABLE MEMBER
(
mem_id INT PRIMARY KEY,
NAME VARCHAR(100) NOT NULL,
pass VARCHAR(12) NOT NULL,
CHECK(CHAR_LENGTH(pass)>7),
ph_no CHAR(11) NOT NULL,
gender VARCHAR(10) NOT NULL,
CHECK(gender='Female' OR gender='Male'),
address VARCHAR(50) NOT NULL,
age INT NOT NULL,
email VARCHAR(50) NOT NULL,
pkg_id INT DEFAULT 0,
FOREIGN KEY (pkg_id) REFERENCES packages(pkg_id)
)
create table mpd --Member Package Details
(
mem_id int primary key foreign key references member(mem_id),
nob int not null default 2,
dur int not null default 14
)
create table borrowing
(
bor_id int primary key,
mem_id int foreign key references member(mem_id) not null,
book_id int foreign key references book(book_id),
iss_date date not null,
ret_date date
)

Cannot create 2 foreign key

I tried to create 2 foreign keys in customerdetails table but it keeps me showing an error when I execute this query. Is there any problem with my query?
CREATE TABLE `vg_shippingdb`.`customerdetails` (
`CustomerID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(25) NOT NULL ,
`Age` INT NOT NULL ,
`Sex` VARCHAR(10) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`CustomerID`),
FOREIGN KEY (ReservedID) REFERENCES reservationdetails(ReservedID),
FOREIGN KEY (TicketID) REFERENCES ticket(TicketID)) ENGINE = InnoDB;
CREATE TABLE `vg_shippingdb`.`reservationdetails` (
`ReservedID` INT NOT NULL AUTO_INCREMENT ,
`ReservedDate` DATE NOT NULL ,
`DepartureTime` VARCHAR(25) NOT NULL ,
`Destination` VARCHAR(45) NOT NULL ,
`Accommodation` TEXT NULL ,
`NameOfVessel` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`ReservedID`)) ENGINE = InnoDB;
CREATE TABLE `vg_shippingdb`.`ticket` (
`TicketID` INT NOT NULL AUTO_INCREMENT ,
`TicketNo` VARCHAR(25) NOT NULL ,
`Status` VARCHAR(25) NOT NULL ,
PRIMARY KEY (`TicketID`)) ENGINE = InnoDB;
The columns does not exist in a table really. Specifying the foreign key does not create a column, you must do it explicitly before the FK definition.
CREATE TABLE `vg_shippingdb`.`customerdetails` (
`CustomerID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(25) NOT NULL ,
`Age` INT NOT NULL ,
`Sex` VARCHAR(10) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`CustomerID`),
-- Create columns
ReservedID INT,
TicketID INT,
-- Then use them in FK expressions
FOREIGN KEY (ReservedID) REFERENCES reservationdetails(ReservedID),
FOREIGN KEY (TicketID) REFERENCES ticket(TicketID)) ENGINE = InnoDB;
PS. The table which FK is refferred on must be created firstly - FK cannot refer the table which will be created later.
before creating foreign keys, create all tables
First create reservationdetails and ticket then create customerdetails.
Note: First create source/referred table then create table with FOREIGN KEY

MySQL Error Code 1452 Cannot add or update a child row

Using these tables
CREATE TABLE people(
idPeople INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100),
PRIMARY KEY(idPeople)
);
CREATE TABLE country_details (
idCountry INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
country VARCHAR(45)
);
CREATE TABLE registration_details(
idRegistration INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
register_date DATETIME NOT NULL
);
CREATE TABLE registration(
idRegistration INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
idPeople INT UNSIGNED NOT NULL DEFAULT 0,
idCountry INT UNSIGNED NOT NULL DEFAULT 0,
register_date_id INT UNSIGNED NOT NULL DEFAULT 0,
name VARCHAR(45) NOT NULL DEFAULT '',
email VARCHAR(45) NOT NULL DEFAULT '',
register_date VARCHAR(45) NOT NULL DEFAULT '',
country VARCHAR(45) NOT NULL DEFAULT '',
FOREIGN KEY(idCountry) REFERENCES country_details(idCountry),
FOREIGN KEY(register_date_id) REFERENCES
registration_details(register_date_id),
FOREIGN KEY(idPeople) REFERENCES people(idPeople)
);
I am getting the error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (peopledb.registration, CONSTRAINT registration_ibfk_1 FOREIGN KEY (idCountry) REFERENCES country_details (idCountry))
Anyone know what could be happening?
Foreign keys ensure that a parent table (in your case country_details) holds a value that is replicated in the child table (registration).
What's happening is that you're trying to input data into registration that attempts to reference a country id that doesn't exist in the country_details table.

SQL-query error date

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