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 created a realistic table set using the ER model for MySQL. I am attempting to do joins, but seem to either be missing something, or not doing them correctly in order to produce NULL values. Below are my created tables:
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| FilmNumber | char(3) | NO | PRI | NULL | |
| FilmName | varchar(60) | YES | | NULL | |
| OpeningDate | varchar(20) | YES | | NULL | |
| TopBilledActor | char(60) | YES | | NULL | |
| Genre | varchar(20) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| CriticSiteNumber | char(3) | NO | PRI | NULL | |
| CriticSiteName | varchar(30) | YES | | NULL | |
| CriticName | char(50) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
+------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+-------+
| FilmNumber | char(3) | YES | MUL | NULL | |
| CriticSiteNumber | char(3) | YES | MUL | NULL | |
| OverallScore | int(5) | YES | | NULL | |
+------------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
My question is, what type of join query would I need in order to produce NULL values, or does something in my table need to be modified because each of the queries I attempt do not return those values.
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!
I have the following existing table in a mysql database:
+---------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------+----------------+
| f1 | int(11) | NO | PRI | NULL | auto_increment |
| field2 | int(11) | NO | MUL | NULL | |
| field3 | int(11) | YES | | NULL | |
| field4 | int(11) | YES | | NULL | |
| field6 | varchar(64) | YES | | NULL | |
| field7 | varchar(16) | YES | | NULL | |
| field8 | varchar(32) | YES | | NULL | |
| field9 | varchar(128) | YES | | NULL | |
| field10 | varchar(128) | YES | | NULL | |
| field11 | varchar(128) | YES | | NULL | |
| field12 | varchar(64) | YES | | NULL | |
| field13 | varchar(32) | YES | | NULL | |
| field14 | varchar(32) | YES | | NULL | |
| field15 | int(11) | YES | MUL | NULL | |
| field16 | date | YES | | NULL | |
| field17 | date | YES | | NULL | |
| field18 | int(11) | YES | MUL | NULL | |
| field19 | varchar(64) | YES | | NULL | |
| field20 | varchar(64) | YES | | NULL | |
| field21 | varchar(16) | YES | | NULL | |
| field22 | varchar(20) | YES | | NULL | |
| field23 | varchar(1000) | YES | | NULL | |
| field24 | int(11) | NO | MUL | NULL | |
| field25 | int(11) | NO | | 0 | |
| field26 | decimal(19,2) | YES | | 0.00 | |
| field27 | decimal(19,2) | YES | | 0.00 | |
| field28 | int(11) | YES | MUL | NULL | |
| field29 | int(11) | YES | MUL | NULL | |
| field30 | varchar(128) | YES | | NULL | |
| field31 | varchar(128) | YES | | NULL | |
| field32 | varchar(16) | YES | | NULL | |
| field33 | int(11) | YES | | NULL | |
| field34 | int(11) | YES | | NULL | |
| field35 | varchar(128) | YES | | NULL | |
| field36 | int(11) | YES | MUL | NULL | |
| field37 | int(11) | YES | | NULL | |
+---------------------+---------------+------+-----+---------+----------------+
I try the following statement to add another row and I'm getting the following error:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
Here are the ways I've tried to insert the row into the table:
insert into table (Field, Type, Null, Key, Default, Extra) VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
insert into table VALUES ('contract_expiration','date','YES','','NULL','');
Both return the same error. There are no triggers on the table, I'm not sure what's going on.
Any suggestions? I'm relatively new to mysql administration, I know a bit but this has me stumped and searches for solutions have turned up nothing.
Any help that could be provided would be MUCH appreciated!
NULL is not a valid field name in:
insert into `table`(Field, Type, Null, Key, Default, Extra)
VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
And Key and default are reserved words. Try this:
insert into `table`(Field, `Type`, `Null`, `Key`, `Default`, Extra)
VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
mysql> desc classroom;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| clId | int(11) | NO | PRI | NULL | auto_increment |
| clFName | varchar(30) | NO | | NULL | |
| clSName | varchar(10) | NO | | NULL | |
| clCapc | int(3) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from classroom;
+------+------------+---------+--------+
| clId | clFName | clSName | clCapc |
+------+------------+---------+--------+
| 1 | Classroom1 | cl1 | 100 |
| 2 | 2 | 2 | 2 |
| 3 | 3f | 3s | 3 |
| 4 | 3f | 3s | 3 |
| 5 | class4 | class4 | 100 |
+------+------------+---------+--------+
5 rows in set (0.00 sec)
I also have same error
mysql> insert into classroom values('Gudadhe', 'Akash', 20);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
This error can be solved by specifying column names before inserting directly as follows
By writing classroom(clFName, clSName, clCapc) we are specifying columns into which we want to insert values
mysql> insert into classroom(clFName, clSName, clCapc) values('Gudadhe', 'Akash', 20);
Query OK, 1 row affected (0.06 sec)
I had a similar case, where I took the table definition from a dev server and the data from a live server, and it turned out that they were actually not quite the same.
The way I found out the difference was (there are probably smarter ways to do it, but this is how I did it):
SHOW CREATE TABLE mytable;
I ran this on all 3 cases (live database, dev database, and new database I created using CREATE TABLE xxx like xxx).
Then I simply compared the 3 and found that the live and dev had a different set of columns, so I simply ran
ALTER TABLE xxx DROP yyy;
until the new table was the same as the table the dump was from; then I could import data.
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