![](/img/trans.png)
[英]How to insert data from one table to another using transactions in SQL?
[英]How to insert data into a table using SQL?
將以下值插入到App.settings表中:
(99, DEFAULT, "horizontal", "2015-09-15 04:01:04")
我有一個名為App的數據庫,帶有一個設置表。 我正在嘗試插入表格中,但似乎無法正確處理。
我的聲明:
INSERT INTO App.settings
VALUES(99, DEFAULT, "horizontal", "2015-09-15 04:01:04");
我做對了嗎? 它說我的答案是錯誤的。
mysql> DESC App.settings;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| user_id | int(7) | NO | | NULL | |
| email_frequency | tinyint(2) unsigned | YES | | NULL | |
| layout | varchar(70) | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
使用insert
,請始終列出表中的列。 其次,默認的字符串定界符是SQL中的單引號而不是雙引號。
因此,我希望看到:
INSERT INTO App.settings (col1, col3, col4) -- your real column names here
VALUES (99, 'horizontal', '2015-09-15 04:01:04');
請注意, col2
已從INSERT
和VALUES
刪除,因為您似乎想要一個DEFAULT
值。 並非所有數據庫都支持該語法。
您要確定元組的所有字段都在那,如果不輸入元組的所有字段,則它們的順序正確,您應該使用以下形式: INSERT INTO App.settings(value, config, position, date ) VALUES(99, "DEFAULT", "horizontal", "2015-09-15 04:01:04");
以相同的順序
這僅是一個示例,我不知道您必須使用的字段名稱必須更改
就像@GordonLinoff所說的那樣,您應該在查詢中包括這些列。 這樣,您還可以跳過輸入email_frequency
的DEFAULT
值。
至於錯誤,很可能是您當前使用的雙引號而不是單引號。
請嘗試以下操作:
INSERT INTO App.settings (user_id, layout, updated_at)
VALUES (99, 'horizontal', '2015-09-15 04:01:04');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.