MySQL选择默认值

时间:2017-06-23 14:54:37

标签: mysql sql

我在一个表中有外键,在另一个表上有引用,而不是null。如何为其选择默认值? 像这样:

ALTER TABLE table_a MODIFY COLUMN not_null_column BIGINT NOT NULL DEFAULT
    (SELECT id FROM table_b WHERE name_field = 'some name');

或者这个:

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;

1 个答案:

答案 0 :(得分:0)

一种选择是使用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)

示例db-fiddle

相关问题