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?
Related
I am using MariaDB, and I am experiencing trouble with introducing a CONSTRAINT.
My version of MariaDB:
Ver 15.1 Distrib 10.1.37-MariaDB
My error message:
ERROR 1005 (HY000): Can't create table `schedulingGUI`.`#sql-1043_1a` (errno: 150 "Foreign key constraint is incorrectly formed")
My table entry for city:
CREATE TABLE city
(
cityId INT unsigned NOT NULL AUTO_INCREMENT,
city VARCHAR(50),
countryId INT unsigned,
customerName VARCHAR(50),
address VARCHAR(50),
postalCode VARCHAR(50),
phone VARCHAR(50),
createDate VARCHAR(50),
createdBy VARCHAR(50),
lastUpdateBy VARCHAR(50),
PRIMARY KEY (cityId)
);
My table entry for customer:
CREATE TABLE customer
(
customerId INT unsigned NOT NULL AUTO_INCREMENT,
customerName VARCHAR(50),
addressId INT unsigned,
active INT unsigned,
address VARCHAR(50),
city VARCHAR(50),
postalCode VARCHAR(50),
phone VARCHAR(50),
createDate VARCHAR(50),
createdBy VARCHAR(50),
lastUpdateBy VARCHAR(50),
PRIMARY KEY (customerId)
);
My CONSTRAINT entry:
ALTER TABLE city
ADD CONSTRAINT customerNameChange01
FOREIGN KEY (customerName)
REFERENCES customer (customerName)
ON UPDATE CASCADE
ON DELETE CASCADE;
I recently stumbled across SHOW ENGINE INNODB STATUS. It states the following:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Thank you for your insight.
EDIT: Content added. Corrected the error message.
The following worked:
CREATE INDEX CustomerName ON customer (customerName);
SHOW ENGINE INNODB STATUS; helped a lot.
This is what I have tried:
create table books(bcode int(5) primary key, bname varchar(45));
and
create table customers(cid int(4), cname varchar(20), cadd varchar(40), bcode,
varchar(45), foreign key(bcode) references books(bcode));
After executing the second statement, the following error shows up:
ERROR 1215 (HY000): Cannot add foreign key constraint
I'm having trouble coming up with a solution. Any help is appreciated.
In the first table books you use bcode as integer
But in the second table you use bcode as varchar,
So, right one is
create table customers(cid int(4), cname varchar(20), cadd varchar(40), bcode
int(5), foreign key(bcode) references books(bcode));
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 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