Set column value of this/next auto_increment value - mysql

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)

Related

Converting from VARCHAR to BINARY(16) in MySQL?

I have as table with this column and type:
sales_channel_id BINARY(16)
In the row I have this value: 5DBACA1114B24872ACCFE679037DF670
I have written this value into another table, but this time the table column has the type VARCHAR(255). In the table I see this value: 5dbaca1114b24872accfe679037df670 (no capitals).
Now I have created another table with a column of type BINARY(16). When I make something like this to transform the data from the varchar column to the new column like this:
INSERT INTO
setting_sales_channel (sales_channel_id)
SELECT sales_channel_id from mcn_setting
I get the error: Query 1 ERROR: Data too long for column 'sales_channel_id' at row 1
Why does this happen and how can I transfer the data from the VARCHAR column into the new column which is of type BINARY(16)?
Try this.
change your query to:
INSERT INTO
setting_sales_channel (sales_channel_id)
SELECT CONVERT(sales_channel_id,BINARY(16)) from mcn_setting;
sample (MariaDB)
MariaDB [bernd]> show create table b;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| b | CREATE TABLE `b` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`bbinary` binary(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)
MariaDB [bernd]> show create table bs;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bs | CREATE TABLE `bs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`bvarchar` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
MariaDB [bernd]> SELECT * from b;
Empty set (0.002 sec)
MariaDB [bernd]> SELECT * from bs;
+----+----------------------------------+
| id | bvarchar |
+----+----------------------------------+
| 1 | 5dbaca1114b24872accfe679037df670 |
+----+----------------------------------+
1 row in set (0.000 sec)
MariaDB [bernd]>
MariaDB [bernd]> INSERT INTO b (bbinary)
-> SELECT CONVERT(bvarchar,BINARY(16)) FROM bs;
Query OK, 1 row affected, 1 warning (0.003 sec)
Records: 1 Duplicates: 0 Warnings: 1
MariaDB [bernd]> SELECT * from bs;
+----+----------------------------------+
| id | bvarchar |
+----+----------------------------------+
| 1 | 5dbaca1114b24872accfe679037df670 |
+----+----------------------------------+
1 row in set (0.000 sec)
MariaDB [bernd]>
In my case it worked when using UNHEX now. The result looked like this:
INSERT INTO setting_sales_channel (sales_channel_id)
SELECT UNHEX(sales_channel_id) from setting;

MySQL select default value

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.

How to implement an auto_increment composite primary key with MySQL InnoDB?

I have a table which has a composite primary key made up of one non-auto_increment column and one auto_increment column. The auto_increment column needs to increment individually for each of the non-auto_increment column values (more on this later). The storage engine is InnoDB. I don't wish to lock the table because of performance concerns. After inserting a value, a means to retrieve the last auto_increment value must be available.
The below script works at first, but the last INSERT results in id, checkingaccounts_id is 3, 2, but 1, 2 is desired. This is what I meant by The auto_increment column needs to increment individually for each of the non-auto_increment column values
Trigger and Stored Procedures are both acceptable, and so is a PHP/PDO application solution which somehow mimics the MySQL auto_increment behavior.
mysql> EXPLAIN checkingaccounts;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | varchar(45) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> EXPLAIN checks;
+---------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| checkingaccounts_id | int(11) | NO | PRI | NULL | |
| data | varchar(45) | YES | | NULL | |
+---------------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> INSERT INTO checkingaccounts(id, data) VALUES(0,'bla');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO checkingaccounts(id, data) VALUES(0,'bla');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM checkingaccounts;
+----+------+
| id | data |
+----+------+
| 1 | bla |
| 2 | bla |
+----+------+
2 rows in set (0.00 sec)
mysql> INSERT INTO checks(id,checkingaccounts_id,data) VALUES(0,1,'bla');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO checks(id,checkingaccounts_id,data) VALUES(0,1,'bla');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM checks;
+----+---------------------+------+
| id | checkingaccounts_id | data |
+----+---------------------+------+
| 1 | 1 | bla |
| 2 | 1 | bla |
+----+---------------------+------+
2 rows in set (0.00 sec)
mysql> INSERT INTO checks(id,checkingaccounts_id,data) VALUES(0,2,'bla');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM checks;
+----+---------------------+------+
| id | checkingaccounts_id | data |
+----+---------------------+------+
| 1 | 1 | bla |
| 2 | 1 | bla |
| 3 | 2 | bla |
+----+---------------------+------+
3 rows in set (0.00 sec)
mysql>
Remove the auto_increment feature, try a stored procedure instead:
CREATE PROCEDURE insertChecks
(IN AccID int(9), IN data varchar(50))
BEGIN
DECLARE cid INT DEFAULT 1;
SELECT (COUNT(*) + 1) INTO cid
FROM checks
WHERE checkingaccounts_id = AccID;
INSERT INTO checks(id, checkingaccounts_id, data)
VALUES(cid, AccID, data);
END
And
call insertChecks(1,'bla');
call insertChecks(1,'bla');
call insertChecks(2,'bla');
Solution 2:
CREATE PROCEDURE insertChecks
(IN AccID int(9), IN data varchar(50))
BEGIN
INSERT INTO checks(id, checkingaccounts_id, data)
SELECT (COUNT(*) + 1), AccID, data
FROM checks
WHERE checkingaccounts_id = AccID;
END
Create a MyISAM table to create the auto_increment id only, and use a trigger to use this id for the targeted table. If there are more than one InnoDB tables that need a composite auto_incrementing primary key, add an extra primary key to the MyISAM table.
Disadvantages:
No foreign key constraints allowed on the MyISAM table, however, hopefully a trigger removes the risk.
Requires an additional table.
Advantages
Primary key valves are not reused if deleted.
Client just uses normal SQL queries and not a stored procedure.
Maybe less maintenance than a stored procedure since adding a column to a table doesn't require the trigger to be modified.
-- MySQL Script generated by MySQL Workbench
-- 07/08/16 05:12:11
-- Model: New Model Version: 1.0
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`a`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`a` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1` (
`a_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`, `a_id`),
CONSTRAINT `fk_t1_a1`
FOREIGN KEY (`a_id`)
REFERENCES `mydb`.`a` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t2` (
`a_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`, `a_id`),
CONSTRAINT `fk_t2_a1`
FOREIGN KEY (`a_id`)
REFERENCES `mydb`.`a` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t3`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t3` (
`a_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`, `a_id`),
CONSTRAINT `fk_t3_a1`
FOREIGN KEY (`a_id`)
REFERENCES `mydb`.`a` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`inc`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`inc` (
`a_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
`type` CHAR(4) NOT NULL,
PRIMARY KEY (`a_id`, `type`, `id`))
ENGINE = MyISAM;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
USE `mydb`;
DELIMITER $$
USE `mydb`$$
CREATE TRIGGER `t1_BINS` BEFORE INSERT ON `t1` FOR EACH ROW
BEGIN
INSERT INTO inc(a_id,type) VALUES(NEW.a_id,'t1');
SET NEW.id=LAST_INSERT_ID();
END$$
USE `mydb`$$
CREATE TRIGGER `t2_BINS` BEFORE INSERT ON `t2` FOR EACH ROW
BEGIN
INSERT INTO inc(a_id,type) VALUES(NEW.a_id,'t2');
SET NEW.id=LAST_INSERT_ID();
END$$
USE `mydb`$$
CREATE TRIGGER `t3_BINS` BEFORE INSERT ON `t3` FOR EACH ROW
BEGIN
INSERT INTO inc(a_id,type) VALUES(NEW.a_id,'t3');
SET NEW.id=LAST_INSERT_ID();
END$$
DELIMITER ;
Testing
mysql> insert into a(id) VALUES(null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1(a_id) VALUES(1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t1;
+------+----+
| a_id | id |
+------+----+
| 1 | 1 |
+------+----+
1 row in set (0.00 sec)
mysql> insert into t1(a_id) VALUES(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1(a_id) VALUES(1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t1;
+------+----+
| a_id | id |
+------+----+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
+------+----+
3 rows in set (0.00 sec)
mysql> insert into t2(a_id) VALUES(1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t2;
+------+----+
| a_id | id |
+------+----+
| 1 | 1 |
+------+----+
1 row in set (0.00 sec)
mysql> insert into a(id) VALUES(null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a(id) VALUES(null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1(a_id) VALUES(2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1(a_id) VALUES(3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1(a_id) VALUES(1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t1;
+------+----+
| a_id | id |
+------+----+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 3 | 1 |
+------+----+
6 rows in set (0.00 sec)
mysql>

MySQL Automatic ID column

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)

MySQL Insert Select - NOT NULL fields

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