2017-06-23 196 views
0

我有一個表中的外鍵,另一個表上的引用不爲null。我怎樣才能選擇它的默認值? 事情是這樣的:MySQL選擇默認值

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

可能重複的[mysql設置字段默認值爲其他列](https://stackoverflow.com/questions/15384429/mysql-set-field-default-value-to-other-column) –

+2

嗯,我想我真的意味着這一個:[MySQL:從另一個表列的列的默認值](https://stackoverflow.com/questions/41102020/mysql-default-value-of-column-from-another-table-column)但看起來第一個表明真正的答案也是如此:這是不可能的。 –

+0

是的,就是這個。謝謝。沒有想到將函數或表達式定義爲默認值是不可能的,因此,在搜索時跳過了這個問題。 – Squeez

回答

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