I am trying to add records into two tables below,
CREATE TABLE customer
(Custno CHAR(3),
Custname VARCHAR(25) NOT NULL,
Custstreet VARCHAR(30) NOT NULL,
Custcity VARCHAR(15) NOT NULL,
Custprov VARCHAR(3) NOT NULL,
Custpcode VARCHAR(6) NOT NULL,
Disc DECIMAL(3,1),
Balance DECIMAL(7,2),
Credlimit DECIMAL(5),
Srepno CHAR(3),
CONSTRAINT pkcustno PRIMARY KEY (Custno),
CONSTRAINT fksrepno FOREIGN KEY (Srepno) REFERENCES salesrep(Srepno)
);
CREATE TABLE orders
(Orderno CHAR(5) UNIQUE NOT NULL,
Orderdate DATE,
Custno CHAR(3) NOT NULL,
CONSTRAINT fkordercust FOREIGN KEY (Custno) REFERENCES customer (Custno)
);
When adding like this,
INSERT INTO orders(Orderno, Orderdate, Custno) VALUES('14587','2011-11-09', '125' );
INSERT INTO orders(Orderno, Orderdate, Custno) VALUES('11547','2011-11-07', '125' );
I get, "Cannot add or update a child row: a foreign key constraint fails (sh.orders, CONSTRAINT fkordercust FOREIGN KEY (Custno) REFERENCES customer (Custno))
"
Is something wrong the table?
You do not have a customer with CustNo = '125'. Because of this, the Foreign key fails. You are trying to place an order for a non-existent customer, the DB throws an error.
Do you actually have a customer row for customer number 125? I think not. The error message is telling you exactly what's wrong.
The foreign key constraint which ensures that no orders can be created for non-existent customers is being violated:
CONSTRAINT fkordercust FOREIGN KEY (Custno) REFERENCES customer (Custno)
Create the customer row first then you can add order rows for that customer to your heart's content.
Your table is fine, you just don't have a customer with a CustNo of '125' in the database.
You have created a foreign key to the customer table, but ( apparently ) inserted no data into it.
Do you have a customer with a number of 125?
Related
I'am trying to add foreign key from department table to employee table. First one is done, But I can't create department table, it pops up error
ERROR 1005 (HY000): Can't create table `assignment`.`department` (errno: 150 "Foreign key constraint is incorrectly formed")
Like this.
CREATE TABLE employee
(
First_Name VARCHAR(15) NOT NULL,
Mid_Name CHAR,
Last_Name VARCHAR(15) NOT NULL,
SSN_Number CHAR(9) PRIMARY KEY NOT NULL,
Birthday DATE,
Address VARCHAR(50),
Sex CHAR CHECK(Sex='M' OR Sex='F' OR Sex='m' OR Sex='f'),
Salary Decimal(10,2) DEFAULT'800',
Supervisor_SSN CHAR(9),
Department_Number INT,
CONSTRAINT fk_employee FOREIGN KEY(Supervisor_SSN)
REFERENCES employee(SSN_Number) ON DELETE SET NULL
);
CREATE TABLE department
(
Department_Name VARCHAR(15) NOT NULL UNIQUE,
Department_Number INT PRIMARY KEY NOT NULL,
Manaager_SSN CHAR(9) NOT NULL,
Manager_Start_Date Date,
CONSTRAINT fk_manager FOREIGN KEY(Manaager_SSN)
REFERENCES employee(SSN_Number) ON DELETE SET NULL
);
I expect to add foreign key on Manaager_SSN to SSN_Number in employee table.
The option ON DELETE SET NULL is not valid if the column is declared NOT NULL. You're telling it to set the column to an impossible value.
So either change the declaration of Manaager_SSN to NULL, or change the foreign key to ON DELETE CASCADE. The former is probably more appropriate -- if the manager of a department leaves the company, you don't usually dissolve the department.
The foreign key definition includes
... ON DELETE SET NULL
^^^^^^^^
but the foreign key column is defined as non-NULL
Manaager_SSN CHAR(9) NOT NULL,
^^^^^^^^
DROP TABLE IF EXISTS CARD_ACCOUNT;
Create Table CARD_ACCOUNT(
acct_no Char(16),
exp_date date,
card_type ENUM('Debit','Credit') NOT NULL,
cust_ID integer NOT NULL
);
DROP TABLE IF EXISTS DEBIT_CARD;
Create Table DEBIT_CARD(
acct_no Char(16),
exp_date date,
bank_no CHAR(9) NOT NULL,
Constraint debit_card_pk primary key(acct_no,exp_date),
Constraint debit_card_fk foreign key(acct_no,exp_date) References card_account(Acct_no,exp_date)
ON UPDATE CASCADE
ON DELETE CASCADE
);
When I try to run this statement I get a "Cannot add foregin key constraint" error in Mysql on the Debit_Card table, why do I get this error the script I am learning from has everything writen the same exact way as I have.
card_account(Acct_no,exp_date) must be Primary Key if you want reference to it in Foreign Key.
and why you don't make it into 1 table?
Create Table CARD_ACCOUNT(
acct_no Char(16),
exp_date date,
bank_no CHAR(9) NOT NULL,
card_type ENUM('Debit','Credit') NOT NULL,
cust_ID integer NOT NULL,
Constraint CARD_ACCOUNT_PK primary key(acct_no,exp_date)
);
i think it serve the same purpose. you already have card_type to know if its debit or credit card so why make separate table for that?
Use the below code, I have just added one primary key constraint in the first table, it will allow you to create the foreign key. But, as "Tim Biegeleisen" commented there is database design problem, you should think again about your database design.
DROP TABLE IF EXISTS CARD_ACCOUNT;
Create Table CARD_ACCOUNT(
acct_no Char(16),
exp_date date,
card_type ENUM('Debit','Credit') NOT NULL,
cust_ID integer NOT NULL,
Constraint debit_card_pk primary key(acct_no,exp_date)
);
DROP TABLE IF EXISTS DEBIT_CARD;
Create Table DEBIT_CARD(
acct_no Char(16),
exp_date date,
bank_no CHAR(9) NOT NULL,
Constraint debit_card_pk primary key(acct_no,exp_date),
Constraint debit_card_fk foreign key(acct_no,exp_date) References card_account(Acct_no,exp_date)
ON UPDATE CASCADE
ON DELETE CASCADE
);
Below is my table-
create table employee1(
fname varchar(15) not null,
minit char(15) ,
lname varchar(15) not null,
ssn char(9) not null,
bdate date,
address varchar(30),
sex char,
salary decimal(10,2),
superssn char(9),
dno int not null default 1,
constraint emppk
primary key (ssn),
constraint empsupkerfk
foreign key (superssn) references employee1(ssn) on delete set null on update cascade,
constraint empdeptfk
foreign key (dno) references department(dnumber) on delete set default on update cascade
);
Here is my insert statment-
insert into employee1
(
fname,minit,lname,ssn,bdate,address,sex,salary,superssn,dno
)
values (
'jhon','b','smith','123456789','1955-01-09','731 fondren,houston, tx','m','30000','33344555','5'
)
This is the error what I am getting-
Error Code: 1452. Cannot add or update a child row: a foreign key
constraint fails (employee.employee, CONSTRAINT employee_ibfk_1
FOREIGN KEY (superssn) REFERENCES employee (ssn)
The error is quite clear and it means that you are trying to insert into employee1 a superssn value that does not exist in employee.
From the docs:
Foreign key relationships involve a parent table that holds the
central data values, and a child table with identical values pointing
back to its parent. The FOREIGN KEY clause is specified in the child
table.
It will reject any INSERT or UPDATE operation that attempts to create
a foreign key value in a child table if there is no a matching
candidate key value in the parent table.
I created two tables students and orders and I added a foreign key constraint to the order table but I forgot to add on delete cascade option to this table.
table STUDENTS:
CREATE TABLE STUDENTS (
ID varchar(50) NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
)
Table ORDERS
CREATE TABLE Orders
(O_Id int NOT NULL PRIMAY KEY,
Order_No int NOT NULL,ID varchar(50))
Add foreign key to "orders":
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID)
I tried this attempt :
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID) ON DELETE CASCADE
Thanks.
You have a typo in the Order table. You have PRIMAY where it should be PRIMARY.
After correcting this, I tried creating the tables and all statements worked fine, including the last one.
I created two tables in mysql,
customer
house table with houseID being foreign key in my customer table.
Create customer table(
id int not null primary key auto_increment,
name varchar not null,
houseId int not null,
telephoneNo, int not null,
CONSTRAINT FOREIGN KEY (houseId) REFERENCES house(id) ON DELETE CASCADE);
CREATE house table(id int not null primary key auto_increment,
houseNo int not null,
address varchar not null);
However, when I delete customer with a specific houseId, the row in house table doesn't get deleted though I put on delete cascade in the customer table. Any idea why?
Your foreign key is on the wrong table. The way you got it set up is that if you delete a house, the corresponding cutomer will be cascaded.
You will want to put a customerId foreign key in the house table and have ON DELETE CASCADE foreign key trigger from side.
With a foreign key the ON DELETE is asking: what if the foreign key I am referencing is deleted (cascade, set null, do nothing)? not what to do when this row get's deleted.