I am using the below create table to create a posts table in mysql :
mysql> create table posts(
-> post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> author_id INT NOT NULL,
-> title varchar(50),
-> post TEXT,
-> data DATE,
-> FOREIGN KEY (author_id)
-> REFERENCES authors(author_id)
-> ON DELETE RESTRICT)ENGINE=INNODB;
But i always get this error:
ERROR 1005 (HY000): Can't create table 'myFirstBlog.posts' (errno: 150)
The describe of my authors table is as follows:
mysql> desc authors;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| author_id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(16) | YES | | NULL | |
| password | varchar(40) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
Can someone please help me with where the error is .
Thanks in advance.
create table posts(
post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
author_id INT NOT NULL,
title varchar(50),
post TEXT,
data DATE,
FOREIGN KEY (author_id)
REFERENCES authors(author_id)
ON DELETE RESTRICT
)ENGINE=INNODB;
Kindly make datatype of both table's column same.
authors(author_id) and post(author_id)
Or
Make authors table first and then execute above query.
I had tried below query it's worked for me.
create table posts(
post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
author_id INT NOT NULL,
title varchar(50),
post TEXT,
data DATE,
FOREIGN KEY (author_id)
REFERENCES labels(id)
ON DELETE RESTRICT
)ENGINE=INNODB;
Hope this will helps.
Related
--------------
CREATE DATABASE kiros_ACMEOnline
--------------
Query OK, 1 row affected (0.00 sec)
Database changed
--------------
CREATE TABLE ITEM(
Item_Number INT UNSIGNED AUTO_INCREMENT,
Item_Name VARCHAR(35) NOT NULL,
Description VARCHAR(255),
Model VARCHAR(50) NOT NULL,
Price VARCHAR(9) NOT NULL,
CONSTRAINT item_pk PRIMARY KEY (Item_Number))
--------------
Query OK, 0 rows affected (0.01 sec)
--------------
DESCRIBE ITEM
--------------
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| Item_Number | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Item_Name | varchar(35) | NO | | NULL | |
| Description | varchar(255) | YES | | NULL | |
| Model | varchar(50) | NO | | NULL | |
| Price | varchar(9) | NO | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
--------------
CREATE TABLE CUSTOMER(
CustomerID INT UNSIGNED NOT NULL,
Address VARCHAR(150) NOT NULL,
Email VARCHAR(80),
business_or_home ENUM('b', 'h'),
CONSTRAINT customer_pk PRIMARY KEY (CustomerID))
--------------
Query OK, 0 rows affected (0.02 sec)
--------------
DESCRIBE CUSTOMER
--------------
+-------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+-------+
| CustomerID | int(10) unsigned | NO | PRI | NULL | |
| Address | varchar(150) | NO | | NULL | |
| Email | varchar(80) | YES | | NULL | |
| business_or_home | enum('b','h') | YES | | NULL | |
+-------------------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
--------------
CREATE TABLE ORDERED(
OrderID INT UNSIGNED AUTO_INCREMENT,
total_cost VARCHAR(11),
CONSTRAINT ordered_pk PRIMARY KEY(OrderID),
CONSTRAINT ordered_CustomerID_fk FOREIGN KEY(CustomerID)
REFERENCES CUSTOMER(CustomerID))
--------------
ERROR 1072 (42000): Key column 'CustomerID' doesn't exist in table
--------------
DESCRIBE ORDERED
--------------
ERROR 1146 (42S02): Table 'kiros_acmeonline.ordered' doesn't exist
--------------
CREATE TABLE LINE_ITEM(
Item_Number INT UNSIGNED,
quantity TINYINT(255),
CONSTRAINT line_item_pk PRIMARY KEY (Item_Number, OrderID),
CONSTRAINT line_item_fk FOREIGN KEY (Item_Number) REFERENCES ITEM(Item_Number),
CONSTRAINT line_item_fk FOREIGN KEY (OrderID) REFERENCES ORDERED(OrderID))
--------------
ERROR 1072 (42000): Key column 'OrderID' doesn't exist in table
--------------
DESCRIBE LINE_ITEM
--------------
ERROR 1146 (42S02): Table 'kiros_acmeonline.line_item' doesn't exist
--------------
CREATE TABLE HOME(
CreditCardNum char(16) NOT NULL,
CreditCardExpirationDate char(6) NOT NULL,
CONSTRAINT home_pk PRIMARY KEY (CustomerID),
CONSTRAINT home_fk FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID))
--------------
ERROR 1072 (42000): Key column 'CustomerID' doesn't exist in table
--------------
DESCRIBE HOME
--------------
ERROR 1146 (42S02): Table 'kiros_acmeonline.home' doesn't exist
--------------
CREATE TABLE BUSINESS(
PaymentTerms VARCHAR(50) NOT NULL,
CONSTRAINT business_pk PRIMARY KEY (CustomerID),
CONSTRAINT business_fk FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID))
--------------
ERROR 1072 (42000): Key column 'CustomerID' doesn't exist in table
--------------
DESCRIBE BUSINESS
--------------
ERROR 1146 (42S02): Table 'kiros_acmeonline.business' doesn't exist
mysql>
When you create a foreign key, the column must be specified, in addition to the foreign key constraint. For example, when you create the table ORDERED you should use the following SQL statement:
CREATE TABLE ORDERED (
OrderID INT UNSIGNED AUTO_INCREMENT,
total_cost VARCHAR(11),
CONSTRAINT ordered_pk PRIMARY KEY(OrderID),
CustomerId int, -- added the column
CONSTRAINT ordered_CustomerID_fk FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID)
)
As you see in your code, you forgot to actually add the column CustomerId.
The same mistake is present in the subsequent constraints.
I have created two tables
EMPLOYEE
CREATE TABLE employee (
emp_id INT AUTO_INCREMENT NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
PRIMARY KEY (emp_id)
);
TEAM
CREATE TABLE team (
team_id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(20),
manager_id INT (20),
PRIMARY KEY (team_id)
);
I am trying to add a add a foreign key:
ALTER TABLE employee ADD FOREIGN KEY (manager_id) REFERENCES team(manager_id);
It is giving me an error telling me that the column does not exist
ERROR 1072 (42000): Key column 'manager_id' doesn't exist in table
But it does show when I Describe team
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| team_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| manager_id | int(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
Column manager_id does not exists in table employee.
Something like this would be more meaningful:
ALTER TABLE team ADD FOREIGN KEY (manager_id) REFERENCES employee(emp_id);
Possibly, you also build a table to represent the relationship between employees and teams. As of now, nothing in your schema can be used to relate an employee to one or several teams.
I intend to create a table with two columns (bus_id, bus_passengers). The bus_id is going to be the primary key and will be the foreign key from another created table, which is called "beacap_locationLog", column node_id.
This is the code I wrote (mySQL):
CREATE TABLE Bus(
bus_id INT (10) UNSIGNED NOT NULL,
bus_passengers INT,
PRIMARY KEY (bus_id),
FOREIGN KEY (bus_id) REFERENCES beacap_locationLog(node_id)
);
It's giving me this error:
#1005 - Can't create table 'pei.Bus' (errno: 150)
I don't know what the problem is.
Cannot have the primary + foreign key on the same ! column with the same name
Shoul be like this !
-see i have added a new column !!
CREATE TABLE Bus(
bus_id INT (10) UNSIGNED NOT NULL,
bus_passengers INT,
node_id_fk int,
PRIMARY KEY (bus_id),
FOREIGN KEY (node_id_fk) REFERENCES beacap_locationLog(node_id)
);
also the node_id_fk and node_id must have the same type !
Ok , see full example :
mysql> create table beacap_locationLog(
-> node_id int
-> );
Query OK, 0 rows affected (0.26 sec)
mysql> CREATE TABLE Bus(
-> bus_id INT (10) UNSIGNED NOT NULL,
-> bus_passengers INT,
-> node_id_fk int,
-> PRIMARY KEY (bus_id),
-> FOREIGN KEY (node_id_fk) REFERENCES beacap_locationLog(node_id)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc beacap_locationLog;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| node_id | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> desc Bus;
+----------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+-------+
| bus_id | int(10) unsigned | NO | PRI | NULL | |
| bus_passengers | int(11) | YES | | NULL | |
| node_id_fk | int(11) | YES | MUL | NULL | |
+----------------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
TBL Bus definition
mysql> show create table Bus\G
Table: Bus
Create Table: CREATE TABLE Bus (
bus_id int(10) unsigned NOT NULL,
bus_passengers int(11) default NULL,
node_id_fk int(11) default NULL,
PRIMARY KEY (bus_id),
KEY node_id_fk (node_id_fk)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
I have created a table which has a foreign key as follows:
CREATE TABLE interests
(
int_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
an_interest VARCHAR(50) NOT NULL,
contact_id INT NOT NULL,
CONSTRAINT my_contacts_contact_id_fk
FOREIGN KEY (contact_id)
REFERENCES my_contacts (contact_id)
);
When I do DESC I see:
mysql> desc interests;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| int_id | int(11) | NO | PRI | NULL | auto_increment |
| interest | varchar(50) | NO | | NULL | |
| contact_id | int(11) | NO | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)
I know what MUL is but how would I explicitely see that contact_id is defined as a foreign key and which is the parent table via CLI?
Also why can't I use my_contacts_contact_id_fk to drop the foreign key constraint?
UPDATE
mysql> ALTER TABLE interests DROP CONSTRAINT my_contacts_contact_id_fk;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n
ear 'CONSTRAINT my_contacts_contact_id_fk' at line 2
mysql>
Constraints are database objects in themselves so it doesn't surprise me that the full description of the constraint is not shown when you run
desc [interests].
Try desc [my_contacts_contact_id_fk]?
You should be able to execute alter table [interests] drop constraint [my_contacts_contact_id_fk]. If you are having trouble dropping can you post your alter table SQL and the response from the server?
I have a table which looks like this:
mysql> SHOW COLUMNS FROM Users;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| user_id | int(10) | NO | PRI | NULL | auto_increment |
| username | varchar(50) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| phone | varchar(255) | YES | | NULL | |
I am trying to create a new table like this:
create table jobs (id int, FOREIGN KEY (user_id) REFERENCES Users(user_id)) ENGINE=INNODB;
But I am getting this error:
ERROR 1072 (42000): Key column 'user_id' doesn't exist in table
I am sure I am missing something very basic.
Try this:
create table jobs (
id int,
user_id int,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
) ENGINE=INNODB;
The first user_id in foreign key constraint refers to the table where the contraint is defined and the second refers to the table where it is pointing to.
So you need a field user_id in your jobs table, too.
This is the script you need:
CREATE TABLE jobs
(
id int NOT NULL,
user_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES Users(user_id)
)
Here's a good reference to learn the basics about setting up relationships: SQL FOREIGN KEY Constraint
You're trying to define as FOREIGN KEY, a column which is not present on your query.
That's the reason why you are receiving Key column 'user_id' doesn't exist in table
Observe your query:
create table jobs (
id int,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
) ENGINE=INNODB;
As the other answers have demonstrated:
you have to define the creation of the column of your new table, which will be a FK for a PK from another table
create table jobs (
id int,
user_id int NOT NULL
FOREIGN KEY (user_id) REFERENCES Users(user_id)
) ENGINE=INNODB;
Create table attendance:
CREATE TABLE tbl_attendance
(
attendence_id INT(100) NOT NULL,
presence varchar(100) NOT NULL,
reason_id varchar(100) NULL,
PRIMARY KEY (attendance_id)
);