Error number 150 in the Foreign Key - mysql

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)

Related

I'm having trouble creating tables relations I can print out one entity but the rest wont run? mySQL 5.7 on unix terminal

--------------
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.

mysql primary key null value

I read that Primary key should be Unique and not null. Now when I create a table checking with depid as primary key, the table description says this according to me : the Null column value NO indicates that depid can't be Null but at the same time the Default column value Null says that default is Null
mysql> desc checking;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| depid | int(11) | NO | PRI | NULL | |
| deppp | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.05 sec)
mysql> insert into checking values(1,2);
Query OK, 1 row affected (0.12 sec)
mysql> insert into checking(deppp) values(1);
ERROR 1364 (HY000): Field 'depid' doesn't have a default value
If that is the case why is the last query producing an error?
Edit The error got resolved once primary key column is labelled as Auto Increment. But that still doesn't explain why the Default column shows Null for a primary key column.
I think you forgot adding the "auto increment" to your primary key, so there is no value added. Create your table like this :
CREATE TABLE checking (
depid int NOT NULL AUTO_INCREMENT,
deppp int(11),
PRIMARY KEY (depid)
);

file sort being used for query and I don't see why (mysql 5)

mysql> EXPLAIN SELECT * FROM
(`phppos_modules`)
JOIN `phppos_permissions` ON `phppos_permissions`.`module_id`=`phppos_modules`.`module_id`
WHERE `phppos_permissions`.`person_id` = '1'
ORDER BY `sort` asc;
+----+-------------+--------------------+--------+-------------------+---------+---------+------------------------------------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+--------+-------------------+---------+---------+------------------------------------+------+----------------+
| 1 | SIMPLE | phppos_modules | ALL | PRIMARY | NULL | NULL | NULL | 11 | Using filesort |
| 1 | SIMPLE | phppos_permissions | eq_ref | PRIMARY,person_id | PRIMARY | 306 | pos.phppos_modules.module_id,const | 1 | Using index |
+----+-------------+--------------------+--------+-------------------+---------+---------+------------------------------------+------+----------------+
2 rows in set (0.00 sec)
mysql> show create table phppos_modules;
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_modules | CREATE TABLE `phppos_modules` (
`name_lang_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`desc_lang_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`sort` int(10) NOT NULL,
`icon` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`module_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`module_id`),
UNIQUE KEY `desc_lang_key` (`desc_lang_key`),
UNIQUE KEY `name_lang_key` (`name_lang_key`),
KEY `sort` (`sort`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> show create table phppos_permissions;
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_permissions | CREATE TABLE `phppos_permissions` (
`module_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`person_id` int(10) NOT NULL,
PRIMARY KEY (`module_id`,`person_id`),
KEY `person_id` (`person_id`),
CONSTRAINT `phppos_permissions_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `phppos_employees` (`person_id`),
CONSTRAINT `phppos_permissions_ibfk_2` FOREIGN KEY (`module_id`) REFERENCES `phppos_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Likely you are being affected by this condition from the documentation:
The key used to fetch the rows is not the same as the one used in the
ORDER BY:
SELECT * FROM t1 WHERE key2=constant ORDER BY key1;
Try the following:
ALTER TABLE `phppos_modules`
ADD INDEX `all_w_sort_first` (`sort`,`module_id`,`name_lang_key`,`desc_lang_key`,`icon`);

Trying to remove a primary key from MySQL table

Edit: Not sure why this is marked as a duplicate. The error I am getting is different
I am trying to remove a primary key definition but am receiving an error for some reason.
mysql> ALTER TABLE `aux_sponsors` DROP PRIMARY KEY;
ERROR 1091 (42000): Can't DROP 'PRIMARY'; check that column/key exists
mysql> desc aux_sponsors;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| unit | varchar(8) | NO | | MF | |
| code | varchar(32) | NO | PRI | NULL | |
| userid | varchar(32) | NO | | | |
| fullName | varchar(64) | NO | | | |
| department | varchar(255) | NO | | | |
| description | varchar(255) | NO | | NULL | |
+-------------+--------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
Am I doing something wrong here? I simply want no more primary key in this table.
mysql> SHOW CREATE TABLE aux_sponsors;
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| aux_sponsors | CREATE TABLE `aux_sponsors` (
`unit` varchar(8) NOT NULL DEFAULT 'MF',
`code` varchar(32) NOT NULL,
`userid` varchar(32) NOT NULL DEFAULT '',
`fullName` varchar(64) NOT NULL DEFAULT '',
`department` varchar(255) NOT NULL DEFAULT '',
`description` varchar(255) NOT NULL,
UNIQUE KEY `code` (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
You don't have a PRIMARY KEY; you have a UNIQUE key. So, you can't do this:
ALTER TABLE `aux_sponsors` DROP PRIMARY KEY
Instead, just do
ALTER TABLE `aux_sponsors` DROP KEY `code`
DESC (a/k/a DESCRIBE) is not a true MySQL feature; according to the docs, "The DESCRIBE statement is provided for compatibility with Oracle."
More from the documentation:
A UNIQUE index may be displayed as PRI if it cannot contain NULL values and there is no PRIMARY KEY in the table. A UNIQUE index may display as MUL if several columns form a composite UNIQUE index; although the combination of the columns is unique, each column can still hold multiple occurrences of a given value.
In your case, the column code is NOT NULL and is the only column in a UNIQUE key, so DESC is showing it as PRI. Because of this type of problem, it's better to use SHOW INDEX to find out the types of keys on a table.

Get MySQL Unique Key Combos

If I have a table created similarly to the following:
CREATE TABLE MyTable(
id1Part1 INT NOT NULL,
id1Part2 INT NOT NULL,
id2Part1 INT NOT NULL,
id2Part2 INT NOT NULL,
UNIQUE KEY (id1Part1, id1Part2),
UNIQUE KEY (id2Part1, id2Part2)
);
how can I now ask the database to give me the two "unique key" tuples?
(SHOW INDEX doesn't seem to do this.)
I'm not sure if you're looking for something like this
select
constraint_name,
group_concat(column_name order by ordinal_position) as cols
from information_schema.key_column_usage
where table_schema = 'db_name' and table_name = 'table_name'
group by constraint_name
mysql can't give you two primary keys. Check this out:
mysql> CREATE TABLE MyTable(
-> id1Part1 INT NOT NULL,
-> id1Part2 INT NOT NULL,
->
-> id2Part1 INT NOT NULL,
-> id2Part2 INT NOT NULL,
->
-> UNIQUE KEY (id1Part1, id1Part2),
-> UNIQUE KEY (id2Part1, id2Part2)
-> );
Query OK, 0 rows affected (0.17 sec)
mysql> desc mytable;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id1Part1 | int(11) | NO | PRI | NULL | |
| id1Part2 | int(11) | NO | PRI | NULL | |
| id2Part1 | int(11) | NO | MUL | NULL | |
| id2Part2 | int(11) | NO | | NULL | |
+----------+---------+------+-----+---------+-------+
It shows there is only one primary key.