SQL composite key syntax - mysql

I am trying to create a composite key for MYSQL, but i dont know how to, ive tried the following;
CREATE TABLE order_line(
order_id int NOT NULL AUTO_INCREMENT,
car_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (order_id, car_id));
ALTER TABLE order_line ADD CONSTRAINT order_line_FK FOREIGN KEY (order_id) REFERENCES orders (order_id);
ALTER TABLE order_line ADD CONSTRAINT order_line_FK2 FOREIGN KEY (car_id) REFERENCES car (car_id);

you cant have autoincremented key.
See this example of yours makes no sense, on the key you are saying that order_id references orders (order_id), which is fine, but on the table you are saying that this same value is auto-increment, which means that it will be generated by the database.
Remove both AUTO_INCREMENT from your order_line table and you should be fine
if you want you can have a AUTO_INCREMENT order_line_id field on your order_line table as a primary key (which I recommend)

In MySQL you can have only one AUTO_INCREMENT column so you can't define a composite key as AUTO_INCREMENT. Have you tried just
CREATE TABLE order_line(
order_id int NOT NULL,
car_id int NOT NULL,
PRIMARY KEY (order_id, car_id));

Okay, i was able to do it like;
CREATE TABLE order_line (
order_id int NOT NULL,
car_id int NOT NULL);
ALTER TABLE order_line ADD CONSTRAINT order_line_PK PRIMARY KEY (order_id, car_id);
ALTER TABLE order_line ADD CONSTRAINT order_line_FK FOREIGN KEY (order_id) REFERENCES orders (order_id);
ALTER TABLE order_line ADD CONSTRAINT order_line_FK2 FOREIGN KEY (car_id) REFERENCES car (car_id);

CONSTRAINT composite_key_name PRIMARY KEY (col1,col2)

Related

Changing the Reference value of a Foreign Key

I have created a table 'company' having the column actor_id referencing the primary key (id) of the table 'actor'. Now I want to change the column actor_id to reference the primary key (id) of the table director. After trying to drop the foreign key, and creating a new one referencing the new table, it did not work as desired, it stayed the same - referencing the previous (actor) table. What changes should I make?
CREATE TABLE company(
id INT NOT NULL AUTO_INCREMENT,
actor_id INT NOT NULL,
CONSTRAINT aID FOREIGN KEY (actor_id) REFERENCES actor(id),
PRIMARY KEY(id)
);
ALTER TABLE company
DROP FOREIGN KEY aID,
ADD CONSTRAINT actID FOREIGN KEY (actor_id) REFERENCES director(id);
Don't see any apparent issues in the code above. I tried same in SQL Fiddle which works. You can check the link.
http://www.sqlfiddle.com/#!9/82e334c/2/0

mysql error 1822 when trying to create new table with a FOREIGN KEY

trying the following code:
create table donation(
donation_number int not null primary key ,
product_id int not null
);
create table stock (
product_id int not null primary key,
available_qty int not null,
FOREIGN KEY (product_id ) REFERENCES donation(product_id)
);
Give back
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'stock_ibfk_1' in the referenced table 'donation'.
Why? how can I solve this problem?
to create a foreign key relationship, the parent table column on which you are creating relation must be unique or primary and they must have the same datatype and size also
product_id in donation table is not unique.

Cannot add or update a child row: foreign key with mysql

I created two tables students and orders, I tried to add a foreign key to the table "orders" but i have this error :
Cannot add or update a child row: a foreign key constraint fails
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)
Check if [Orders] table currently has any IDs that are not in [STUDENTS] table.
I think that you are trying to add foreign key constraint and there is no corresponding primery key in parent table.
Try to run
SET FOREIGN_KEY_CHECKS=0
your ALTER TABLE statement. When you finish run
SET FOREIGN_KEY_CHECKS=1

How to add on delete cascade option in mysql?

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.

Foreign Key After Table Creation

I have already created a table in MySQL! And have tried a number of queries to alter the table and add foreign key to the table!
But none of them work??
No error message no nothing but still nothing happening...
I need the exact query which would work! :(
Details:
Table1: users column: id
Table2: Pokemon_ref column: pkmn_id
If an insertion is done in Table1 then it should also be added in Table2!
sample:
ALTER TABLE tablename
ADD CONSTRAINT FK_Name_ID FOREIGN KEY (fk_ID)
REFERENCES (R_id);
https://dev.mysql.com/doc/refman/5.1/en/create-table-foreign-keys.html
http://www.w3schools.com/sql/sql_foreignkey.asp
Alter table to give foreign key constraint
category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)
) ENGINE=INNODB;
CREATE TABLE customer (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id)
REFERENCES customer(id)
)