I have foreign key in one table, references on another table, not null. How can I select the default value for it?
Something like this:
ALTER TABLE table_a MODIFY COLUMN not_null_column BIGINT NOT NULL DEFAULT
(SELECT id FROM table_b WHERE name_field = 'some name');
Or this:
SET #defaultValue = (SELECT id FROM table_b WHERE name_field = 'some name');
ALTER TABLE table_a MODIFY COLUMN not_null_column BIGINT NOT NULL DEFAULT #defaultValue;
One option is to use 13.5 Prepared SQL Statement Syntax.
mysql> DROP TABLE IF EXISTS `table_b`, `table_a`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `table_a` (
-> `not_null_column` VARCHAR(20)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `table_b` (
-> `name_field` VARCHAR(255) NOT NULL,
-> `value` VARCHAR(255) NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `table_b`
-> (`name_field`, `value`)
-> VALUES
-> ('some name', '5');
Query OK, 1 row affected (0.00 sec)
mysql> DESC `table_a`;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| not_null_column | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> SET #`stmt` := CONCAT('ALTER TABLE `table_a`
'> MODIFY COLUMN `not_null_column` BIGINT NOT NULL
'> DEFAULT ', (SELECT `value`
-> FROM `table_b`
-> WHERE `name_field` = 'some name'));
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT #`stmt`;
+-------------------------------------------------------------------------------------------------------------------------------+
| #`stmt` |
+-------------------------------------------------------------------------------------------------------------------------------+
| ALTER TABLE `table_a`
MODIFY COLUMN `not_null_column` BIGINT NOT NULL
DEFAULT 5 |
+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> PREPARE `stmt` FROM #`stmt`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE `stmt`;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)
mysql> DESC `table_a`;
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| not_null_column | bigint(20) | NO | | 5 | |
+-----------------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
Example db-fiddle.
Related
CREATE PROCEDURE p_samp(
IN p_id INT,IN p_table_choice VARCHAR(10)
)
BEGIN
CASE p_table_choice
WHEN p_table_choice = 'A' THEN
USE database1;
update sample1
SET name = 'sam'
WHERE id = p_id;
WHEN p_table_choice = 'B' THEN
USE database2;
update sample2
SET name = 'sam'
WHERE id = p_id;
ELSE
BEGIN
END;
END CASE ;
END;
You can try:
CREATE PROCEDURE p_samp(
IN p_id INT,IN p_table_choice VARCHAR(10)
)
BEGIN
CASE p_table_choice
WHEN p_table_choice = 'A' THEN
update database1.sample1
SET name = 'sam'
WHERE id = p_id;
WHEN p_table_choice = 'B' THEN
update database2.sample2
SET name = 'sam'
WHERE id = p_id;
ELSE
BEGIN
END;
END CASE ;
END;
I tried a sample procedure and it worked.
Here's a sample procedure I tried to perform a similar update between two databases learning and pricing >>
CREATE PROCEDURE `xxxx`(A int(1))
begin
case A
when 1 then update learning.GAUL set user='OBELIX' where id=1;
when 0 then update pricing.K1 set amntIN='500' where account=1;
else
select 'DUMMY';
END CASE;
END
So basically the first update shall result in one affected row and the 2nd one shall result in 2 affected rows.
I call them:
mysql> call xxxx(1);
Query OK, 1 row affected (0.05 sec)
mysql> call xxxx(0);
Query OK, 3 rows affected (0.12 sec)
mysql> call xxxx(3);
+-------+
| DUMMY |
+-------+
| DUMMY |
+-------+
1 row in set (0.00 sec)
The following script works as expect:
mysql> DROP PROCEDURE IF EXISTS `p_samp`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `database2`.`sample2`,
-> `database1`.`sample1`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP DATABASE IF EXISTS `database2`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP DATABASE IF EXISTS `database1`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `database1`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `database2`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `database1`.`sample1` (
-> `id` SERIAL,
-> `name` VARCHAR(255)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `database2`.`sample2` (
-> `id` SERIAL,
-> `name` VARCHAR(255)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `database1`.`sample1`
-> (`name`)
-> VALUES
-> ('sam in db1');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO `database2`.`sample2`
-> (`name`)
-> VALUES
-> ('sam in db2');
Query OK, 1 row affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `p_samp` (
-> `p_id` BIGINT UNSIGNED,
-> `p_table_choice` CHAR(1)
-> )
-> BEGIN
-> CASE `p_table_choice`
-> WHEN 'A' THEN
-> UPDATE `database1`.`sample1`
-> SET `name` = 'sam'
-> WHERE `id` = `p_id`;
-> WHEN 'B' THEN
-> UPDATE `database2`.`sample2`
-> SET `name` = 'sam'
-> WHERE `id` = `p_id`;
-> END CASE;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `database1`.`sample1`;
+----+------------+
| id | name |
+----+------------+
| 1 | sam in db1 |
+----+------------+
1 row in set (0.00 sec)
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `database2`.`sample2`;
+----+------------+
| id | name |
+----+------------+
| 1 | sam in db2 |
+----+------------+
1 row in set (0.00 sec)
mysql> CALL `p_samp`(1, 'A');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `database1`.`sample1`;
+----+------+
| id | name |
+----+------+
| 1 | sam |
+----+------+
1 row in set (0.00 sec)
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `database2`.`sample2`;
+----+------------+
| id | name |
+----+------------+
| 1 | sam in db2 |
+----+------------+
1 row in set (0.00 sec)
mysql> CALL `p_samp`(1, 'B');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `database1`.`sample1`;
+----+------+
| id | name |
+----+------+
| 1 | sam |
+----+------+
1 row in set (0.00 sec)
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `database2`.`sample2`;
+----+------+
| id | name |
+----+------+
| 1 | sam |
+----+------+
1 row in set (0.00 sec)
CREATE FUNCTION `getSequenceNumber` (
company_id INTEGER, sequence_name varchar(255)) RETURNS INT(10)
BEGIN
INSERT INTO sequences (`company_id`, `name`, `value`)
VALUES (company_id, sequence_name, LAST_INSERT_ID(1))
ON DUPLICATE KEY UPDATE
`value` = LAST_INSERT_ID(value + 1);
RETURN LAST_INSERT_ID(); END
CREATE TABLE `sequences` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`company_id` int(10) unsigned NOT NULL,
`value` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `sequences_name_company_id_unique` (`name`,`company_id`),
KEY `sequences_company_id_index` (`company_id`),
KEY `sequences_value_index` (`value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
//sample output
MariaDB [testdb]> select version();
+----------------+
| version() |
+----------------+
| 10.2.6-MariaDB |
+----------------+
1 row in set (0.00 sec)
MariaDB [testdb]> select `getSequenceNumber`(1,'sequence_021');
+---------------------------------------+
| `getSequenceNumber`(1,'sequence_021') |
+---------------------------------------+
| 2 |
+---------------------------------------+
1 row in set (0.03 sec)
MariaDB [testdb]> select `getSequenceNumber`(1,'sequence_0212');
+----------------------------------------+
| `getSequenceNumber`(1,'sequence_0212') |
+----------------------------------------+
| 5 |
+----------------------------------------+
1 row in set (0.03 sec)
MariaDB [testdb]> select `getSequenceNumber`(1,'new_sequence123');
+------------------------------------------+
| `getSequenceNumber`(1,'new_sequence123') |
+------------------------------------------+
| 6 |
+------------------------------------------+
1 row in set (0.03 sec)
i have this function my MariaDB and it works, but the problem is when inserting, the new ID it insert is <last_id> + <last value> is there a way to clear/refresh the LAST_INSERT_ID before inserting a record?
EDIT: added create sql statement and sample output
I can't reproduce the problem:
MariaDB [_]> SELECT VERSION();
+----------------+
| VERSION() |
+----------------+
| 10.2.6-MariaDB |
+----------------+
1 row in set (0.00 sec)
MariaDB [_]> DROP TABLE IF EXISTS `sequences`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaMariaDB [_]> CREATE TABLE IF NOT EXISTS `sequences` (
-> `name` VARCHAR(255) PRIMARY KEY,
-> `value` BIGINT UNSIGNED NOT NULL
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [_]> DROP FUNCTION IF EXISTS `getSequenceNumber`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE OR REPLACE FUNCTION `getSequenceNumber` (
-> `sequence_name` VARCHAR(255)
-> )
-> RETURNS BIGINT UNSIGNED
-> BEGIN
-> INSERT INTO `sequences` (`name`, `value`)
-> VALUES (`sequence_name`, LAST_INSERT_ID(1))
-> ON DUPLICATE KEY UPDATE `value` = LAST_INSERT_ID(`value` + 1);
-> RETURN LAST_INSERT_ID();
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> SELECT `getSequenceNumber`('sequence_0'); -- 1
+-----------------------------------+
| `getSequenceNumber`('sequence_0') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_0'); -- 2
+-----------------------------------+
| `getSequenceNumber`('sequence_0') |
+-----------------------------------+
| 2 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_1'); -- 1
+-----------------------------------+
| `getSequenceNumber`('sequence_1') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.01 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_2'); -- 1
+-----------------------------------+
| `getSequenceNumber`('sequence_2') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_0'); -- 3
+-----------------------------------+
| `getSequenceNumber`('sequence_0') |
+-----------------------------------+
| 3 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_2'); -- 2
+-----------------------------------+
| `getSequenceNumber`('sequence_2') |
+-----------------------------------+
| 2 |
+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`('sequence_1'); -- 2
+-----------------------------------+
| `getSequenceNumber`('sequence_1') |
+-----------------------------------+
| 2 |
+-----------------------------------+
1 row in set (0.00 sec)
UPDATE
MariaDB [_]> SELECT VERSION();
+----------------+
| VERSION() |
+----------------+
| 10.2.6-MariaDB |
+----------------+
1 row in set (0.00 sec)
MariaDB [_]> DROP FUNCTION IF EXISTS `getSequenceNumber`;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP TABLE IF EXISTS `sequences`;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `sequences` (
-> `name` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
-> `company_id` BIGINT UNSIGNED unsigned NOT NULL,
-> `value` BIGINT UNSIGNED NOT NULL,
-> PRIMARY KEY `sequences_name_company_id_unique` (`name`, `company_id`),
-> KEY `sequences_company_id_index` (`company_id`),
-> KEY `sequences_value_index` (`value`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE OR REPLACE FUNCTION `getSequenceNumber` (
-> `_company_id` BIGINT UNSIGNED,
-> `sequence_name` VARCHAR(255)
-> ) RETURNS BIGINT UNSIGNED
-> BEGIN
-> INSERT INTO `sequences` (`name`, `company_id`, `value`)
-> VALUES (`sequence_name`, `_company_id`, LAST_INSERT_ID(1))
-> ON DUPLICATE KEY UPDATE `value` = LAST_INSERT_ID(`value` + 1);
-> RETURN LAST_INSERT_ID();
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_021'); -- 1
+----------------------------------------+
| `getSequenceNumber`(1, 'sequence_021') |
+----------------------------------------+
| 1 |
+----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(2, 'sequence_021'); -- 1
+----------------------------------------+
| `getSequenceNumber`(2, 'sequence_021') |
+----------------------------------------+
| 1 |
+----------------------------------------+
1 row in set (0.01 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_021'); -- 2
+----------------------------------------+
| `getSequenceNumber`(1, 'sequence_021') |
+----------------------------------------+
| 2 |
+----------------------------------------+
1 row in set (0.02 sec)
MariaDB [_]> SELECT `getSequenceNumber`(2, 'sequence_021'); -- 2
+----------------------------------------+
| `getSequenceNumber`(2, 'sequence_021') |
+----------------------------------------+
| 2 |
+----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_0212'); -- 1
+-----------------------------------------+
| `getSequenceNumber`(1, 'sequence_0212') |
+-----------------------------------------+
| 1 |
+-----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'sequence_021'); -- 3
+----------------------------------------+
| `getSequenceNumber`(1, 'sequence_021') |
+----------------------------------------+
| 3 |
+----------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'new_sequence123'); -- 1
+-------------------------------------------+
| `getSequenceNumber`(1, 'new_sequence123') |
+-------------------------------------------+
| 1 |
+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `getSequenceNumber`(1, 'new_sequence123'); -- 2
+-------------------------------------------+
| `getSequenceNumber`(1, 'new_sequence123') |
+-------------------------------------------+
| 2 |
+-------------------------------------------+
1 row in set (0.01 sec)
I was wondering how to make MySQL automatically have a column that adds 1 to every row that is made (Row 1 will have ID 1, Row 2 will get ID 2, etc.)
For example:
Every time a new user signs up on a website, they are assigned an ID number. Starting at 1, then 2, etc.
ID|Username|Password
1 |Bob |drowssaP
2 |Jill |cats
Try AUTO_INCREMENT. Documentation here
If you add, that magic word :) to your table creation declaration, it will do the magic for you :)
mysql> CREATE TABLE Users (
-> ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Username varchar(255) NOT NULL,
-> Password varchar(255) NOT NULL
-> );
Query OK, 0 rows affected (0.52 sec)
mysql> INSERT INTO Users(Username, Password) VALUES('vladimir', 'ilich_lenin');
Query OK, 1 row affected (0.12 sec)
mysql> INSERT INTO Users(Username, Password) VALUES('friedrich', 'engels');
Query OK, 1 row affected (0.04 sec)
mysql> SELECT * FROM Users;
+----+-----------+-------------+
| ID | Username | Password |
+----+-----------+-------------+
| 1 | vladimir | ilich_lenin |
| 2 | friedrich | engels |
+----+-----------+-------------+
2 rows in set (0.02 sec)
EDIT
mysql> CREATE TABLE Persons ( ID int NOT NULL PRIMARY KEY, Username varchar(255) NOT NULL, Password varchar(255) NOT NULL);
Query OK, 0 rows affected (0.25 sec)
mysql> SHOW CREATE TABLE
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)
mysql> ALTER TABLE Persons MODIFY ID INTEGER NOT NULL AUTO_INCREMENT;
Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE Persons;
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)
mysql> INSERT INTO Persons(Username, Password) VALUES('friedrich', 'engels');
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO Persons(Username, Password) VALUES('karl', 'marx');
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM Persons
-> ;
+----+-----------+----------+
| ID | Username | Password |
+----+-----------+----------+
| 1 | friedrich | engels |
| 2 | karl | marx |
+----+-----------+----------+
2 rows in set (0.00 sec)
Is there any way to accomplish below sql result as a one liner.
create table test( id int not null primary key auto_increment, name char(10));
insert into test (name) values ('voda'+ this_value_of_id);
// so select would return
MariaDB [testdb]> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | foo1 |
+----+------+
Yes, I know the other way is
begin transaction;
insert into test (name) values ('voda');
update test set name = concat('voda', id) where id = 1;
commit;
An option or approach can be via a Virtual (Computed) Columns.
Example:
MariaDB [testdb]> DROP TABLE IF EXISTS `test`;
Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> CREATE TABLE `test` (
-> `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> `name` CHAR(10),
-> `nameid` VARCHAR(20) AS (CONCAT(`name`, `id`)) VIRTUAL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> INSERT INTO `test`
-> (`name`)
-> VALUES
-> ('foo'), ('voda');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [testdb]> SELECT
-> `id`,
-> `nameid` `name`
-> FROM
-> `test`;
+----+-------+
| id | name |
+----+-------+
| 1 | foo1 |
| 2 | voda2 |
+----+-------+
2 rows in set (0.00 sec)
Oh hey there,
I am trying to load data into a table via a INSERT... SELECT statement, but I am having issues with MySQL handling NULL values.
In the below example, table1 is the source and table2 is the destination (Note that table2 has more constraints on the description field):
mysql> drop table if exists table1;
Query OK, 0 rows affected (0.03 sec)
mysql> drop table if exists table2;
Query OK, 0 rows affected (0.00 sec)
mysql> create table if not exists table1 (
-> id int not null auto_increment,
-> description varchar(45),
-> primary key (`id`)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> create table if not exists table2 (
-> id int not null auto_increment,
-> description varchar(45) not null,
-> primary key (`id`),
-> unique index `unique_desc` (`description`)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert ignore into table1
-> (description)
-> values("stupid thing"),
-> ("another thing"),
-> (null),
-> ("stupid thing"),
-> ("last thing");
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from table1;
+----+---------------+
| id | description |
+----+---------------+
| 1 | stupid thing |
| 2 | another thing |
| 3 | NULL |
| 4 | stupid thing |
| 5 | last thing |
+----+---------------+
5 rows in set (0.00 sec)
mysql> insert ignore into table2
-> (description)
-> select description
-> from table1;
Query OK, 4 rows affected, 1 warning (0.01 sec)
Records: 5 Duplicates: 1 Warnings: 1
mysql> select * from table2;
+----+---------------+
| id | description |
+----+---------------+
| 3 | |
| 2 | another thing |
| 4 | last thing |
| 1 | stupid thing |
+----+---------------+
4 rows in set (0.00 sec)
The row with the empty space and id=3 should not be there. I understand that MySQL handles the NOT NULL directive this way by default, but I tried specifying the sql_mode option to "STRICT_ALL_TABLES", which I found to have the following affect:
Without sql_mode set:
mysql> drop table if exists table2;
Query OK, 0 rows affected (0.00 sec)
mysql> create table if not exists table2 (
-> id int not null auto_increment,
-> count int,
-> description varchar(45) not null,
-> primary key (`id`),
-> unique index `unique_desc` (`description`)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into table2
-> (count,description)
-> values(12,"stupid thing");
Query OK, 1 row affected (0.00 sec)
mysql> insert into table2
-> (count)
-> values(5);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from table2;
+----+-------+--------------+
| id | count | description |
+----+-------+--------------+
| 1 | 12 | stupid thing |
| 2 | 5 | |
+----+-------+--------------+
2 rows in set (0.00 sec)
With sql_mode set to "STRICT_ALL_TABLES":
mysql> drop table if exists table1;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> drop table if exists table2;
Query OK, 0 rows affected (0.00 sec)
mysql> create table if not exists table2 (
-> id int not null auto_increment,
-> count int,
-> description varchar(45) not null,
-> primary key (`id`),
-> unique index `unique_desc` (`description`)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into table2
-> (count,description)
-> values(12,"stupid thing");
Query OK, 1 row affected (0.01 sec)
mysql> insert into table2
-> (count)
-> values(5);
ERROR 1364 (HY000): Field 'description' doesn't have a default value
mysql> select * from table2;
+----+-------+--------------+
| id | count | description |
+----+-------+--------------+
| 1 | 12 | stupid thing |
+----+-------+--------------+
1 row in set (0.00 sec)
Note that in the above comparison, if you explicitly give the description field a NULL value, the database will properly complain WITH AND WITHOUT the "STRICT_ALL_TABLES" option set:
mysql> insert into table2
-> (count,description)
-> values(12,null);
ERROR 1048 (23000): Column 'description' cannot be null
Conclusion:
For some reason, setting the sql_mode affects this kind of insert, but does not affect the INSERT... SELECT behavior.
How can I get the data from table1 into table2 with a single query, and no empty cells?
Thanks in advance,
K
Simply use a WHERE clause:
insert ignore into table2(description)
select description from table1
where description <> '' and description is not null