I have two tables
CREATE TABLE members(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(50),
password char(128),
salt char(128),
status VARCHAR(20),
profile VARCHAR(15),
unlock_code INT,
username VARCHAR(20),
privilege VARCHAR(15)
);
CREATE TABLE member_details(
detail_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
middle_name VARCHAR(50),
last_name VARCHAR(50),
contact VARCHAR(12),
dob VARCHAR(10),
nic VARCHAR(15),
mobile VARCHAR(12),
userid INT,
FOREIGN KEY (userid) REFERENCES members(id)
);
How the thing is that when I DESCRIBE TABLE it shows MUL.
Engine is InnoDB.
Also, is it okay to not declare foreign keys and just use JOINS in query and make it act like foreign key?
Keep the database without constraints! Make the columns 'act' like foreign keys but don't but constraints.
Make sure you have sufficient verification in server side scripting to handle those constraints there.
Related
How to create foreignkey constraints in this scenario?
One asterisk- primary key
Two asterisks- foreign key
Tutor table:
**tutorId
firstname
surname
telephoneNo
qualification
employmentDate
introducedBy*
Course table:
**courseCode
courseName
lengthHours
tuitonFee
tutorId*
roomNo*
This is well documented here:
https://www.w3schools.com/sql/sql_foreignkey.asp
I would recommend renaming your columns. Camelcase is one thing, but it is better to either use PascalCase or underscore as separator. Do note that firstname is two words, which should follow your standard. You've also not stated what the reference table is for roomNo or introducedBy, so you will have to edit my code with that information. If they are indeed supposed to be keys, they should probably have ID in the column name.
Fiddle:
https://www.db-fiddle.com/f/6EKuZ7DcCRsAvmrLGYcJrx/1
CREATE TABLE tutor (
tutorId INT NOT NULL,
firstname varchar(50),
surname varchar(50),
telephoneNo varchar(50),
qualification varchar(50),
employmentDate datetime,
introducedBy int,
PRIMARY KEY (tutorId)
/* FOREIGN KEY (introducedBy) REFERENCES ????? */
) ENGINE=INNODB;
CREATE TABLE courseCode (
courceCode int,
courceName varchar(50),
tutorId INT,
lenghtHours INT,
tuitonFee INT,
roomNo varchar(50),
INDEX par_ind (tutorId),
PRIMARY KEY (courceCode),
/* FOREIGN KEY (roomNo) REFERENCES ????? */
FOREIGN KEY (tutorId) REFERENCES tutor(tutorId)
ON DELETE CASCADE
) ENGINE=INNODB;
I know there are numerous questions regarding this error on here but I've searched through many and none seem to explain it in my case!
I've created a table using the following code:
CREATE TABLE Venue (
venueID VARCHAR(20),
venueEmail VARCHAR(30) NOT NULL,
address VARCHAR(100),
phoneNo VARCHAR(20),
managerNo VARCHAR(20),
capacity INT(4),
PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;
And am trying to create a table with a foreign key that refers to the first table using this code:
CREATE TABLE Concert (
referenceNo VARCHAR(6),
venueEmail VARCHAR(30),
eventDate VARCHAR(10),
startTime VARCHAR(5),
ticketsSold INT(4),
PRIMARY KEY (referenceNo),
FOREIGN KEY (venueEmail) REFERENCES Venue ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;
But it's giving me the 1215 error message!
Syntax wise this isn't correct.
The issue's resolved here:
CREATE TABLE Venue (
venueID VARCHAR(20),
venueEmail VARCHAR(30) NOT NULL,
address VARCHAR(100),
phoneNo VARCHAR(20),
managerNo VARCHAR(20),
capacity INT(4),
PRIMARY KEY (venueEmail)
)ENGINE=InnoDB;
CREATE TABLE Concert (
referenceNo VARCaHAR(6),
venueEmail VARCHAR(30),
eventDate VARCHAR(10),
startTime VARCHAR(5),
ticketsSold INT(4),
PRIMARY KEY (referenceNo),
FOREIGN KEY (venueEmail) REFERENCES Venue(`venueEmail`) ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=InnoDB;
Note:
The column being referenced should be stated like table_name(column_name).
You missed the column_name part.
REFERENCE
More:
#Bill Karwin added the following useful info in the comment section:
FWIW this is a MySQL idiosyncrasy. In standard SQL, if you omit the
referenced column name, it defaults to the same name as the foreign
key column. But InnoDB doesn't support this shortcut syntax—you must
specify the column in both cases.
I have to create a number of tables for my project and each one of them has a foreign key besides their primary key. I know how to create a table on putty without the foreign key but if they are all depends on each other, then how to create them individually ?
For example, if I want to create table A, B and C
CREATE TABLE PROFESSOR
(
SSN numeric(9) primary key,
PNAME varchar(20),
CITY varchar(20),
STREETADDRESS varchar(50),
STATE char(2),
ZIP numeric(5),
AREACODE numeric(3),
PHONENUMBER numeric(7),
SEX char(1),
TITLE char(4),
SALARY float(9),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
CREATE TABLE DEPARTMENT
(
DNUM numeric(1) primary key,
DNAME varchar(20),
DPHONE numeric(10),
OFFICELOCATION varchar(20),
foreign key (CWID) references STUDENT(CWID)
);
CREATE TABLE STUDENT
(
CWID numeric(9) primary key,
FNAME varchar(20),
LNAME varchar(20),
SADDRESS varchar(50),
SPHONE numeric(10),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
I'm using Putty to create table and I have to run them separately. The thing is if I run the scrip separately for each table, it's going to give me an error, because the table that has the foreign key does not create yet. How am I going to fix it ? and is there a work around way on solving this problem ? Thank you.
If I run the script for creating table separately, it will give me an error
So I'm having a small problem with creating a table. The problem is in my attempt to create a foreign key to another table. I'm currently using MYSQL2008 Management R2 express, so no designer. Heres my two tables
use teckDB;
CREATE TABLE inventory
(
primId int NOT NULL PRIMARY KEY,
prodName VarChar(255),
quantity int,
prodCost MONEY,
prodDesc VARCHAR(255)
);
CREATE TABLE orderTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT NOT NULL,
created date,
prodId INT,
);
Those two tables ran without a problem. When create the thrid one however causes this error message.
Msg 1769, Level 16, State 1, Line 3 Foreign key 'orderTB' references
invalid column 'orderTB' in referencing table 'CustomerTB'. Msg 1750,
Level 16, State 0, Line 3 Could not create constraint. See previous
errors.
On the thrid table of....
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT, FOREIGN KEY (orderTB) REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
try this
FOREIGN KEY (iparent_id) REFERENCES innodb_parent (iparent_id)
I think this should help to solve your query
http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/
You have an extra comma after "orderId INT", and you have got the foreign key syntax wrong.
This should work:
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
tested on SQLFiddle here
I'm making a database, with several tables, and so far I could make the tables, and update it with some demo data, but when a tried to make a new table for connecting two of my tables I just get this can't create table error whatever I did. Tried to rename the entities to an entirely different name maybe that's the problem but it wasn't.
CREATE DATABASE IF NOT EXISTS ETR;
CREATE TABLE Hallgato (
OktAzonosito INT(6) PRIMARY KEY,
EHAazonosito VARCHAR(11),
TeljesNev VARCHAR(50),
Szemelyazonosito INT(6),
AnyaNyelv VARCHAR(20),
VegzettsegSzint VARCHAR(25),
AnyjaNeve VARCHAR(35),
SzuletesiHely VARCHAR(30),
SzuletesiEv DATE,
Allampolgarsag VARCHAR(30),
Neme VARCHAR(5),
Adoazonosito INT(6),
TAJszam INT(6),
BankszamlaSzam INT(9)
) ENGINE = InnoDB;
CREATE TABLE OktAdat (
OktAzonosito INT(6),
NyelvVizsgaNyelve VARCHAR(15),
NyVegzesDatuma DATE,
NyIntezmeny VARCHAR(35),
EgyebVegzetteseg VARCHAR(15),
EgyVegzesDatuma DATE,
ID INT(5) PRIMARY KEY NOT NULL auto_increment,
FOREIGN KEY (OktAzonosito) REFERENCES Hallgato(OktAzonosito) ON DELETE CASCADE
) ENGINE = InnoDB;
CREATE TABLE Oktato (
TEHAazonosito VARCHAR(11) PRIMARY KEY,
Nev VARCHAR(50),
SzuletesiEv DATE,
Szakterulet VARCHAR(25),
Telefonszam INT(9),
Email VARCHAR(50)
) ENGINE = InnoDB;
CREATE TABLE Kurzus (
KurzusKod VARCHAR(8) PRIMARY KEY,
KurzusNev VARCHAR(30),
Idotartam INT(3),
EloadasHelye VARCHAR(25),
Tipus CHAR(3),
Vizsgatipus VARCHAR(7),
KreditErtek INT(2),
OktatoKod VARCHAR(11)
) ENGINE = InnoDB;
CREATE TABLE Terem (
Sorszam INT(3) PRIMARY KEY NOT NULL,
Epuletszam INT(1),
Kapacitas INT(3),
IRszam INT(4),
Utca CHAR(25),
Hazszam INT(2)
) ENGINE = InnoDB;
CREATE TABLE DiakKurz (
kd_id INT (5) PRIMARY KEY NOT NULL auto_increment,
DKKod VARCHAR(8),
EHA VARCHAR(11),
FOREIGN KEY (EHA) REFERENCES Hallgato(EHAazonosito) ON DELETE CASCADE,
FOREIGN KEY (DKKod) REFERENCES Kurzus(KurzusKod) ON DELETE CASCADE
) ENGINE = InnoDB;
The problem is with creating the DiakKurz table.
You'll have to set an INDEX for column EHAazonosito in table Hallgato
CREATE TABLE Hallgato (
OktAzonosito INT(6) PRIMARY KEY,
EHAazonosito VARCHAR(11), INDEX (EHAazonosito),
TeljesNev VARCHAR(50),
Szemelyazonosito INT(6),
AnyaNyelv VARCHAR(20),
VegzettsegSzint VARCHAR(25),
AnyjaNeve VARCHAR(35),
SzuletesiHely VARCHAR(30),
SzuletesiEv DATE,
Allampolgarsag VARCHAR(30),
Neme VARCHAR(5),
Adoazonosito INT(6),
TAJszam INT(6),
BankszamlaSzam INT(9)
) ENGINE = InnoDB;
This
FOREIGN KEY (EHA) REFERENCES Hallgato(EHAazonosito) ON DELETE CASCADE,
doesn't work because EHAazonosito is not the primary key of the Hallgato table. A foreign key can only reference a primary (or unique) key.
Btw: are you awary that the 6 in int(6) is not about limiting the values in the column?
It doesn't do anything. int(6) is the same as int. The 6 is only there to give client applications a hint(!) with how many digits the column should be displayed. It does not enforce anything at all.
Have you tried looking up the error?
If MySQL reports an error number 1005 from a CREATE TABLE statement,
and the error message refers to error 150, table creation failed
because a foreign key constraint was not correctly formed. Similarly,
if an ALTER TABLE fails and it refers to error 150, that means a
foreign key definition would be incorrectly formed for the altered
table. To display a detailed explanation of the most recent InnoDB
foreign key error in the server, issue SHOW ENGINE INNODB STATUS.
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
it happened also to me after a dump of the whole schema, what helped was to wrap whole dump within disabled foreign key checks
SET foreign_key_checks=0;
...dump
SET foreign_key_checks=1;