I'm a mysql beginner and I'm currently working on foreign keys.
I would like to create three tables: users, items, orders and link them together
Users table:
CREATE TABLE users (
user_id INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
PRIMARY KEY(user_id)
);
Items table:
CREATE TABLE items (
item_id INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
PRIMARY KEY(item_id)
);
Orders table:
CREATE TABLE orders (
order_id INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
item_id INT,
quantity INT(10) NOT NULL,
user_id INT,
PRIMARY KEY (order_id),
FOREIGN KEY (item_id) REFERENCES items (item_id),
FOREIGN KEY (user_id) REFERENCES users (user_id)
);
But I got the error 1005: can't create table 'new.orders' (error:150)
What is wrong with my code?
Thanks!
The data types of the primary table's column and the referencing table's columns must match exactly. In your definitions, items.item_id is INT UNSIGNED, while the foreign key orders.item_id is INT (implicitly SIGNED). The same is true for user_id.
Change the definition as follows, adding UNSIGNED for those two:
CREATE TABLE orders (
order_id INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
item_id INT UNSIGNED,
quantity INT(10) NOT NULL,
user_id INT UNSIGNED,
PRIMARY KEY (order_id),
FOREIGN KEY (item_id) REFERENCES items (item_id),
FOREIGN KEY (user_id) REFERENCES users (user_id)
);
It then builds correctly: http://sqlfiddle.com/#!2/cfab8
There is no column student_id in items table. Did you mean user_id in users table?
Related
this is my code:
CREATE DATABASE exams;
SHOW DATABASES;
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(40),
last_name VARCHAR(40)NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE(email));
SHOW table status
INSERT INTO exams_3121(student_id, first_name, middle_name, last_name, email, password, reg_date)
CREATE TABLE entries
(
entrie_id int NOT NULL,
student_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (subject_id)REFERENCES subject(subject_id)
)
CREATE DATABASE subjects;
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(20) NOT NULL,
level_entery VARCHAR(40)NOT NULL,
exam_board VARCHAR(60) NOT NULL,
PRIMARY KEY (subject_id));
CREATE TABLE entries
(
entrie_id int NOT NULL,
entrie_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (subject_id)REFERENCES subject(subject_id)
)
When I use this code it says cannot add foreign key constraint
and I don't know what to do. Please and thanks in advance.
There are two problems.
First, you got the names of the table you're referencing wrong. The name of the tables are students and subjects, but you wrote student and subject.
Second, the entries table has two entrie_id columns. One of them should be student_id.
CREATE TABLE entries
(
entrie_id int NOT NULL,
student_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (subject_id) REFERENCES subjects(subject_id)
)
Also, if you're creating multiple databases and putting your tables in them, you'll need to refer to tables with their database prefixes if they're different from the one you selected as default with the USE command. As you've written it, you're not actually using the databases you created with CREATE DATABASE.
Below is the database that I'm trying to implement:-
CREATE DATABASE Cinema;
USE Cinema;
-- Person Supertype
CREATE TABLE Person (
ID INT(2) UNSIGNED NOT NULL,
Name VARCHAR(40) NOT NULL,
Age TINYINT(120) UNSIGNED NOT NULL,
PhoneNumber VARCHAR(20),
PRIMARY KEY(ID)
);
-- Customer Subtype
CREATE TABLE Customer (
Customer_ID INT(2) UNSIGNED NOT NULL,
Type enum('Member','Adult','Student','Child'),
PRIMARY KEY(Customer_ID),
FOREIGN KEY(Customer_ID) REFERENCES Person(ID)
);
-- Staff Subtype
CREATE TABLE Staff (
Staff_ID INT(2) UNSIGNED NOT NULL,
SalaryPerMonth DECIMAL(6,2),
PRIMARY KEY(Staff_ID),
FOREIGN KEY(Staff_ID) REFERENCES Person(ID)
);
--Booking enitity
CREATE TABLE Booking (
Booking_ID INT(3) NOT NULL,
Price DECIMAL(3,2),
TicketQuantity INT(100),
PRIMARY KEY(Booking_ID)
);
--Customer_Booking relationship
CREATE TABLE Customer_Booking (
Customer_ID INT(2) UNSIGNED NOT NULL,
Booking_ID INT(3) UNSIGNED NOT NULL,
PRIMARY KEY(Customer_ID, Booking_ID),
FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID),
FOREIGN KEY(Booking_ID) REFERENCES Booking(Booking_ID)
);
The error appears when I enter the above table.
Your booking_id columns in both customer_booking and booking tables are not equal. One is UNSIGNED but other one is not. So you can use the following table creation script for "customer_booking" table
CREATE TABLE Customer_Booking (
Customer_ID INT(2) UNSIGNED NOT NULL,
Booking_ID INT(3) NOT NULL,
PRIMARY KEY(Customer_ID, Booking_ID),
FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID),
FOREIGN KEY(Booking_ID) REFERENCES Booking(Booking_ID)
);
Or
if you want booking_id to be unsigned then correct it in the booking table creation script.
I've read several posts on this type of error (one of them was quite similar), but I haven't found one yet that resolves the error that I'm having with the foreign key. Since I can recreate the error, I think I have a pretty good idea of what it is. I am new to SQL, however, and I can't resolve the problem. Here's the code from the two affected tables:
create table artist (
artist_id int unsigned not null auto_increment,
first_name nvarchar(50) not null,
last_name nvarchar(50) not null,
about_artist nvarchar(550) not null,
start_of_event datetime,
end_of_event datetime,
item_id int unsigned not null,
user_id int unsigned not null,
last_modified timestamp,
primary key (artist_id),
foreign key (item_id) references
items (item_id),
foreign key (user_id) references
users (user_id)
);
create table items (
item_id int unsigned not null auto_increment,
artist_id int unsigned not null,
item_name nvarchar(50) not null,
item_description nvarchar(150) not null,
last_modified timestamp,
primary key (item_id),
foreign key (artist_id) references
artist(artist_id)
);
My item_id is item_id int unsigned not null, in the first table, but item_id int unsigned not null auto_increment, in the second table. The second table is the one where it is the primary key. I can't do two auto increments in one table, and if I reverse the order of the tables, the same thing happens with a different column. How can I resolve this? Thank you.
Thinking about my comment, I'm increasingly confident that the link table is the way you need to go.
create table artist (
artist_id int unsigned not null auto_increment,
first_name nvarchar(50) not null,
last_name nvarchar(50) not null,
about_artist nvarchar(550) not null,
start_of_event datetime,
end_of_event datetime,
user_id int unsigned not null,
last_modified timestamp,
primary key (artist_id),
foreign key (user_id) references
users (user_id)
);
create table items (
item_id int unsigned not null auto_increment,
artist_id int unsigned not null,
item_name nvarchar(50) not null,
item_description nvarchar(150) not null,
last_modified timestamp,
primary key (item_id)
);
create table artist_item (
artist_id int unsigned not null,
item_id int unsigned not null
foreign key (artist_id) references
artist(artist_id),
foreign key (item_id) references
items(item_id)
);
You could add an auto-increment field to artist_item, or have the two ID fields as a composite key.
With this, you can query for all items relating to an artist (e.g. one artist has several items):
select
*
from
artist_item join items on
artist_item.item_id = items.item_id
where
artist_item.artist_id = [ID]
or all artists related to an item (e.g. one item was worked on by several artists):
select
*
from
artist_item join artist on
artist_item.artist_id = artist.artist_id
where
artist_item.item_id = [ID]
Have an error in my SQL code, but could not understand, where it is. Please, help me to solve that. Here is my code:
CREATE TABLE IF NOT EXISTS Records (
record_id int(11) NOT NULL AUTO_INCREMENT,
record_year year(4) NOT NULL,
record_quarter int(1) NOT NULL,
profit_tax int(11) NOT NULL,
PRIMARY KEY (record_id, record_year, record_quarter),
UNIQUE(record_year, record_quarter)
);
CREATE TABLE IF NOT EXISTS ProductsList (
product_id int(11) NOT NULL AUTO_INCREMENT,
product_name varchar(24) NOT NULL,
PRIMARY KEY (product_id)
);
An error is in that table:
CREATE TABLE IF NOT EXISTS RecordsProducts (
recordproduct_id int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (recordproduct_id),
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
);
referencing column should be INDEX
referencing column should have same data type of referenced column
both referencing, referenced table should be InnoDB
CREATE TABLE IF NOT EXISTS RecordsProducts (
...
...
record_id int NOT NULL, <=== check data type
product_id int NOT NULL,
INDEX(record_id), <===== check INDEXed or not
INDEX(product_id), <====
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
) ENGINE = InnoDB; <=== add 'ENGINE=InnoDB' explicitly
I have the following code in mysql.
create table employee(
e_id int(10) not null auto_increment,
user_id int(10),
usertype_id default 1,
name varchar(50),
primary key (e_id)
);
create table relation(
r_id int(10) not null auto_increment,
user_id int(10) not null,
usertype_id int(10) not null,
interest_id int(10) not null,
primary key (id)
);
Firstly, i want user_id will have the same value as column e_id;
And then, i want to add user_id and usertype_id as an unity in table relation as a foreign key for user_id and usertype_id in table employee.
Do you know how to do that?
Thanks a lot.
You can make user_id and usertype_id in table relation as foreign keys for the same columns in table employee like this:
create table relation(
r_id int(10) not null auto_increment,
foreign key user_id references employee(user_id),
foreign key usertype_id references employee(usertype_id),
interest_id int(10) not null,
primary key (id)
);
I can't help you with e_id and user_id being equal to each other. Honestly, it sounds like a waste of space.
You could introduce a trigger to guarantee that e_id and user_id contain the same value, but I agree with the previous poster. What's the point?