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
Related
We want to create an online reservation system for restaurants. Here is our ER Diagram:
So my question is that since we've never created ER Diagram on our own (I'm Student) I'm curious what could we do better or what else should we add. There is one more thing to mention, we want to create Web-Application and Mobile-Application.
And my second question is if there is any resource where I could find the best practices values for VARCHAR, I've been googling it and there are different values each time. E.g. someone said for email the length should be 80 and someone said it should be 255 (default).
Here are our tables for now if the ER Diagram is right this should be right too.
DROP TABLE RESERVATION;
DROP TABLE RESTAURANT;
DROP TABLE CUSTOMER;
DROP TABLE RESTAURANTOwner;
CREATE TABLE CUSTOMER (
CUSTOMER_ID INT primary key NOT NULL,
CUSTOMER_NAME VARCHAR(255),
CUSTOMER_EMAIL VARCHAR(255),
CUSTOMER_USERNAME VARCHAR(80),
CUSTOMER_PASSWORD VARCHAR(80),
CUSTOMER_PHONE VARCHAR(20)
);
CREATE TABLE RESTAURANTOwner (
OWNER_ID INT primary key NOT NULL,
OWNER_NAME VARCHAR(255),
OWNER_EMAIL VARCHAR(255),
OWNER_USERNAME VARCHAR(80),
OWNER_PASSWORD VARCHAR(80),
OWNER_PHONE VARCHAR(20)
);
CREATE TABLE RESTAURANT (
RESTAURANT_ID INT primary key NOT NULL,
OWNER_ID INT NOT NULL,
RESTAURANT_NAME VARCHAR(255),
RESTAURANT_LAYOUT VARCHAR(255),
CONSTRAINT FK_OWNER_ID FOREIGN key (OWNER_ID) references RESTAURANTOwner(OWNER_ID)
);
CREATE TABLE RESERVATION (
RESTAURANT_ID INT NOT NULL,
CUSTOMER_ID INT NOT NULL,
RESERVATION_TIME Time,
RESERVATION_DATE DATE,
PRIMARY KEY(RESTAURANT_ID,CUSTOMER_ID,RESERVATION_TIME,RESERVATION_DATE),
CONSTRAINT FK_CUSTOMER_ID FOREIGN key (CUSTOMER_ID) references CUSTOMER(CUSTOMER_ID),
CONSTRAINT FK_RESTAURANT_ID FOREIGN key (RESTAURANT_ID) references RESTAURANT(RESTAURANT_ID)
);
I have these two tables:
CREATE TABLE Collaborators (
CustomerID INT NOT NULL,
FirstName VARCHAR(25),
LastName VARCHAR(25),
Street VARCHAR(50),
City VARCHAR(50),
State VARCHAR(25),
ZipCode INT,
Telephone VARCHAR(15),
PRIMARY KEY(CustomerID));
CREATE TABLE Orders (
OrderID INT NOT NULL,
CustomerID INT NOT NULL,
SKU VARCHAR(20),
Description VARCHAR(50),
PRIMARY KEY(OrderID),
FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerID));
My goal is to change any instance of "Customer" to "Collaborator". I tried to go one at a time and use an alter table statement.
ALTER TABLE Collaborators CHANGE CustomerID CollaboratorID INT;
ALTER TABLE Orders CHANGE CustomerID CollaboratorID INT;
Here is the error code MySQL spits out at me:
ERROR 1025 (HY000): Error on rename of './QuantigrationUpdates/#sql-668_24' to './QuantigrationUpdates/Collaborators' (errno: 150)
Any help would be greatly appreciated. I've figured that I can't simply change the column names because they have key constraints, but I don't know how to work around that. Thanks.
Update: Am I able to use CREATE VIEW to work around the key constraints?
I am new to SQL and I did everything on my understanding but i can't create the table. says error 1005 can't create table user.EMPLOYEE and user.STORE (errno 150) help
DROP TABLE IF EXISTS EMPLOYEE, STORE, REGION;
CREATE TABLE REGION (
REGION_CODE int NOT NULL AUTO_INCREMENT,
REGION_DESCRIPT varchar(20),
PRIMARY KEY (REGION_CODE)
)Engine=InnoDB;
CREATE TABLE EMPLOYEE (
EMP_CODE int NOT NULL AUTO_INCREMENT,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB date,
STR_CODE int NOT NULL ,
PRIMARY KEY (EMP_CODE,STR_CODE),
FOREIGN key (STR_CODE) REFERENCES STORE(STORE_CODE)
) Engine=InnoDB;
CREATE TABLE STORE (
STORE_CODE int NOT NULL AUTO_INCREMENT,
STORE_NAME varchar(20),
STORE_YRD_SALES numeric,
REGION_CODE int,
EMP_CODE int NOT NULL,
PRIMARY KEY (STORE_CODE),
FOREIGN KEY (REGION_CODE) REFERENCES REGION(REGION_CODE),
FOREIGN KEY (EMP_CODE) REFERENCES EMPLOYEE(EMP_CODE)
) Engine=InnoDB;
Error 150 is an invalid key constraint which, if that's the order you're creating tables in, is probably caused by the fact you can't add a constraint in employee for the currently-not-existing store.
Not being able to create store is simply a side effect of that issue since employee is not created. Once you fix the problem preventing that, the foreign key restraints in store should be okay.
The easiest solution is to probably create employee without the constraint, then add it in later once store exists. This would be something like (note the missing constraint in the EMPLOYEE table creation):
CREATE TABLE EMPLOYEE (
EMP_CODE int NOT NULL AUTO_INCREMENT,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB date,
STR_CODE int NOT NULL,
PRIMARY KEY (EMP_CODE,STR_CODE)
);
CREATE TABLE STORE ( <<no change here>> );
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMP2STORE
FOREIGN KEY (STR_CODE) REFERENCES STORE(STORE_CODE);
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.
I am getting an issue and I feel like I fixed everything. MySQL workbench is still giving me this error. If you can please explain what is wrong I would be greatly appreciate it. I did check for key constraints in the Account table. It is giving the error in the account table.
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
You need to create the referencing table first before creating the table which is referring other tables with FOREIGN key.
The order of table creation should be as
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
You are creating the Foreign key for Member before creating the table so it does not exist yet. Change the order you are creating the tables.
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
Ofcourse the order of table creation is important here.
Create the Member and Employee tables first, followed by the Account. It should work fine.
you have wrong order of creating tables
try this
1- Create member table first
2- employer table second
3- account table in last
http://www.sqlfiddle.com/#!2/47d58