mysql> describe phppos_sales_suspended;
+--------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+----------------+
| sale_time | timestamp | NO | | CURRENT_TIMESTAMP | |
| customer_id | int(10) | YES | MUL | NULL | |
| employee_id | int(10) | NO | MUL | 0 | |
| comment | text | NO | | NULL | |
| sale_id | int(10) | NO | PRI | NULL | auto_increment |
| payment_type | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+-------------------+----------------+
After executing the below on an EMPTY table the primary key seems to be reset back to 1. Why is that? I thought that wasn't possible. This doesn't happen in mysql 5.1.54
ALTER TABLE `phppos_sales_suspended` ADD `deleted` INT( 1 ) NOT NULL DEFAULT '0',
ADD INDEX ( `deleted` );
mysql> describe phppos_sales_suspended;
+--------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+----------------+
| sale_time | timestamp | NO | | CURRENT_TIMESTAMP | |
| customer_id | int(10) | YES | MUL | NULL | |
| employee_id | int(10) | NO | MUL | 0 | |
| comment | text | NO | | NULL | |
| sale_id | int(10) | NO | PRI | NULL | auto_increment |
| payment_type | varchar(255) | YES | | NULL | |
| deleted | int(1) | NO | MUL | 0 | |
+--------------+--------------+------+-----+-------------------+----------------+
Adding an index to an InnoDB table rebuilds the table. Not inheriting the old autoincrement value was a bug that's been fixed in your newer version.
http://bugs.mysql.com/bug.php?id=34920
http://bugs.mysql.com/bug.php?id=21404
Related
op_dc_ip_address_alloc:
Added vlan field in this table.
desc op_dc_ip_address_alloc;
+----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| ip_address | char(40) | NO | MUL | NULL | |
| data_center_id | bigint(20) unsigned | NO | MUL | NULL | |
| pod_id | bigint(20) unsigned | NO | MUL | NULL | |
| nic_id | bigint(20) unsigned | YES | | NULL | |
| reservation_id | char(40) | YES | | NULL | |
| taken | datetime | YES | | NULL | |
| mac_address | bigint(20) unsigned | NO | | NULL | |
| vlan | int(10) unsigned | YES | | NULL | |
+----------------+---------------------+------+-----+---------+----------------+
Host_pod_ref:
desc host_pod_ref;
+------------------+---------------------+------+-----+----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+----------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | MUL | NULL | |
| uuid | varchar(40) | YES | UNI | NULL | |
| data_center_id | bigint(20) unsigned | NO | MUL | NULL | |
| gateway | varchar(255) | NO | | NULL | |
| cidr_address | varchar(15) | NO | | NULL | |
| cidr_size | bigint(20) unsigned | NO | | NULL | |
| description | varchar(255) | YES | | NULL | |
| allocation_state | varchar(32) | NO | MUL | Enabled | |
| external_dhcp | tinyint(4) | NO | | 0 | |
| removed | datetime | YES | MUL | NULL | |
| owner | varchar(255) | YES | | NULL | |
| created | datetime | YES | | NULL | |
| lastUpdated | datetime | YES | | NULL | |
| engine_state | varchar(32) | NO | | Disabled | |
+------------------+---------------------+------+-----+----------+----------------+
These are two tables, i am trying to insert values in op_dc_ip_address_alloc as follows:
INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id, mac_address)
VALUES ('10.147.29.160', 1, 11, (select mac_address from `cloud`.`data_center` where id=1));
I am facing error as
Cannot add or update a child row: a foreign key constraint fails (cloud.op_dc_ip_address_alloc, CONSTRAINT fk_op_dc_ip_address_alloc__pod_id FOREIGN KEY (pod_id) REFERENCES host_pod_ref (id) ON DELETE CASCADE)
can anyone please help me on this.
INSERT INTO cloud.op_dc_ip_address_alloc (ip_address, data_center_id, pod_id, mac_address)
VALUES ('10.147.29.160', 1, 11, (select mac_address from clou``data_center where id=1));
Are you sure exist id=11 in the host_pod_ref table ?
I think this is the reason for the issue.
I have following table called transactions and I want to add foreign key using column account_id. But when I execute the query (see below) 0 rows affected. I am not getting what is wrong with the query.
I have two tables called transactions and accounts. Accounts table has an id as primary key and an account has_many transactions.
transactions table
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| m_number | varchar(255) | YES | | NULL | |
| registration_number | varchar(255) | YES | | NULL | |
| page_number | varchar(255) | YES | | NULL | |
| entry_date | datetime | YES | | NULL | |
| narration | varchar(255) | YES | | NULL | |
| voucher_number | varchar(255) | YES | | NULL | |
| debit_credit | float | YES | | NULL | |
| profit | float | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| balance | float | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| account_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
16 rows in set (0,01 sec)
mysql> ALTER TABLE transactions ADD FOREIGN KEY (account_id) REFERENCES accounts(id);
Query OK, 0 rows affected (0,03 sec)
Records: 0 Duplicates: 0 Warnings: 0
accounts table
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| father_name | varchar(255) | YES | | NULL | |
| nic_number | varchar(255) | YES | | NULL | |
| mohalla | varchar(255) | YES | | NULL | |
| village | varchar(255) | YES | | NULL | |
| nominee | varchar(255) | YES | | NULL | |
| relationship | varchar(255) | YES | | NULL | |
| opening_balance | float | YES | | NULL | |
| opening_date | datetime | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
After running the query shouldn't the column say F_K or something like PRI ?
Any help would be great. Thanks!
This is my tables structures
mysql> DESCRIBE sections;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| sec_id | int(5) | NO | PRI | NULL | auto_increment |
| sec_name | varchar(45) | YES | | NULL | |
| sec_type | tinyint(4) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE subjects;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| sub_id | int(5) | NO | PRI | NULL | auto_increment |
| sub_name | varchar(45) | YES | | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE chapters;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| chp_id | int(5) | NO | PRI | NULL | auto_increment |
| chp_name | varchar(45) | YES | | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE questions;
+-----------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------+------+-----+---------+----------------+
| que_id | int(11) | NO | PRI | NULL | auto_increment |
| que_text | text | YES | | NULL | |
| que_created | timestamp | YES | | NULL | |
| chapters_chp_id | int(5) | NO | MUL | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-----------+------+-----+---------+----------------+
mysql> DESCRIBE answers;
+------------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-----------+------+-----+---------+----------------+
| ans_id | int(11) | NO | PRI | NULL | auto_increment |
| ans_text | text | YES | | NULL | |
| ans_created | timestamp | YES | | NULL | |
| questions_que_id | int(11) | NO | MUL | NULL | |
| chapters_chp_id | int(5) | NO | MUL | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+------------------+-----------+------+-----+---------+----------------+
Let's assume 'Answers' table
I created the 'ans_id' as the primary key of 'Answers' table, created a another UNIQUE key using (ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id)
What i want do.
create primary key for 'Answers' table using (ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id) columns and create Trigger before INSERT to create 'auto_increment' id.
Finally i want to accomplish this
Definition: Primary key is minimal set of attributes needed to determine every row in a table.
UNIQUE index should be {questions_que_id} + {chapters_chp_id} + {subjects_sub_id} +{sections_sec_id} and make sure it is combined as one index with these 4 columns, if you set it as different 4 indexes than you will probably achieve undesired outcome, as Zohar already pointed. From my experience, it could be unpractical to use composite keys like this. For basic table operations (CRUD) you will have to provide a lot of parameters.
Maybe to try this way: autoincremental numeric id as a primary key, the rest of important attributes as UNIQUE index. Then you could count already inserted answers to make sure right number will be assigned to ans_id. Hint: it's maybe easier to achive this from your application layer, not as a trigger or procedure for MySQL. If your users will provide answers as step-by-step, then just save a timestamp and you will know which answer is older so you don't need ans_id at all.
i have a 'results' table that has a datetime colum 'created_time'.
this table reference another table 'results_values' (results.id reference results_values.results_id). here are tables describe:
mysql> describe webforms_results;
+------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| webform_id | int(11) | NO | | NULL | |
| store_id | int(11) | NO | | NULL | |
| customer_id | int(11) | NO | | NULL | |
| customer_ip | bigint(20) | NO | | NULL | |
| created_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
| approved | tinyint(1) | NO | | NULL | |
| view_on_frontend | tinyint(1) | NO | | 0 | |
+------------------+------------+------+-----+---------+----------------+
mysql> describe webforms_results_values;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| result_id | int(11) | NO | MUL | NULL | |
| field_id | int(11) | NO | | NULL | |
| value | text | NO | | NULL | |
| rating | int(11) | YES | | NULL | |
| created_time | datetime | YES | | NULL | |
+--------------+----------+------+-----+---------+----------------+
now i've added a datetime column 'created_time' on 'results_values', and i want to insert datetimes on the second tables copying them from the first table on the corresponding id. is there a quick way to do it?
yes triggers are the solution. And if you want to update already inserted data, you can do :
UPDATE webforms_results_values wrv INNER JOIN webforms_results wr
ON wrv.result_id = wr.id
SET wrv.created_time = wr.created_time
Create "Triggers" to update each and every update of those tables.
Please refer following URL:
Triggers
I'm trying to insert a row into a table named Booklets which has a nullable column BookletSubjectID. The insert is failing because
'BookletSubjectID' cannot be null
Here's my MySQL session copied verbatim. I must be missing something really obvious, but can't see it.
mysql> desc Booklets;
+----------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+----------------+
| BookletID | int(11) | NO | PRI | NULL | auto_increment |
| MemberID | int(11) | NO | MUL | NULL | |
| Name | varchar(255) | YES | | NULL | |
| Description | varchar(1000) | YES | | NULL | |
| RelationshipTypeID | varchar(12) | YES | MUL | NULL | |
| RateTypeID | varchar(2) | YES | MUL | NULL | |
| PrivTypeID | varchar(2) | NO | MUL | NULL | |
| PhotoAlbumID | int(11) | YES | MUL | NULL | |
| VideoAlbumID | int(11) | YES | MUL | NULL | |
| BookletSubjectTypeID | varchar(2) | NO | MUL | NULL | |
| BookletSubjectID | int(11) | YES | MUL | NULL | |
| Deleted | tinyint(1) | NO | | 0 | |
| Credt | datetime | NO | | NULL | |
| Updt | datetime | YES | | NULL | |
+----------------------+---------------+------+-----+---------+----------------+
14 rows in set (0.00 sec)
mysql> INSERT Booklets (MemberID, Name, PrivTypeID, BookletSubjectTypeID, Deleted, Credt)
VALUES (546502, 'dddd','pu', 'no', 1, NOW());
ERROR 1048 (23000): Column 'BookletSubjectID' cannot be null
Thanks,
Don
It seems that:
If a table has a column which is part of a multi-column primary key then that column
cannot be nullable.
See this bug report and its response.