数据表的基本操作(四)数据的插入
文章目錄
- 插入數(shù)據(jù) —— INSERT
- 常見的插入數(shù)據(jù)
- 一.想表中所有的字段插入數(shù)據(jù)
- 實(shí)例1
- 實(shí)例2
- 二.向表中指定字段插入數(shù)據(jù)
- 實(shí)例3
- 三.同時(shí)插入多條數(shù)據(jù)
- 實(shí)例4
- 四.將其他表中的數(shù)據(jù)插入到表中
- 實(shí)例5(1)
- 實(shí)例5(2)
插入數(shù)據(jù) —— INSERT
常見的插入數(shù)據(jù)
- 向表中所有的字段插入數(shù)據(jù)
- 向表中指定字段插入數(shù)據(jù)
- 向表中指定字段插入多條數(shù)據(jù)和將其他表中的數(shù)據(jù)插入到表中
一.想表中所有的字段插入數(shù)據(jù)
1. 指定字段及值
語法:
INSTER INTO 表名 (字段1,字段2,字段3,...)VALUES(數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,...);實(shí)例1
題目:想goods表中插入一條新數(shù)據(jù)。
1.查詢數(shù)據(jù)庫
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | demo_8_4 | | firm | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec)2.創(chuàng)建數(shù)據(jù)庫
mysql> CREATE DATABASE db_shop; Query OK, 1 row affected (0.02 sec)3.再次查看數(shù)據(jù)庫
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | db_shop | | demo_8_4 | | firm | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 7 rows in set (0.00 sec)4.選擇數(shù)據(jù)庫
mysql> USE db_shop; Database changed5.創(chuàng)建數(shù)據(jù)表
mysql> CREATE TABLE goods(-> id INT(11) PRIMARY KEY AUTO_INCREMENT,-> type VARCHAR(30) NOT NULL,-> name VARCHAR(30) UNIQUE,-> price DECIMAL(7,2) UNSIGNED,-> num INT(11) DEFAULT 0,-> add_time DATETIME-> ); Query OK, 0 rows affected, 3 warnings (0.15 sec)6.向goods表中插入一條新記錄
mysql> INSERT INTO goods(id,type,name,price,num,add_time)-> VALUES(1,'書籍','斗羅大陸',38.5,30,'2022-02-12 15:30:27'); Query OK, 1 row affected (0.01 sec)7.查看表中的記錄
mysql> SELECT*FROM goods; +----+--------+--------------+-------+------+---------------------+ | id | type | name | price | num | add_time | +----+--------+--------------+-------+------+---------------------+ | 1 | 書籍 | 斗羅大陸 | 38.50 | 30 | 2022-02-12 15:30:27 | +----+--------+--------------+-------+------+---------------------+ 1 row in set (0.00 sec)- 2.不指定字段值列出字段值。
語法:
INSERT INTO 表名 VALUES(數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,...);實(shí)例2
題目:向goods表中插入一條新記錄。
1.向goods表中插入新數(shù)據(jù)。
mysql> INSERT INTO goods VALUES(2,'水果','火龍果',15,213,'2022-03-06 14:12:32'); Query OK, 1 row affected (0.01 sec)2.查看數(shù)據(jù)結(jié)果
mysql> SELECT*FROM goods; +----+--------+--------------+-------+------+---------------------+ | id | type | name | price | num | add_time | +----+--------+--------------+-------+------+---------------------+ | 1 | 書籍 | 斗羅大陸 | 38.50 | 30 | 2022-02-12 15:30:27 | | 2 | 水果 | 火龍果 | 15.00 | 213 | 2022-03-06 14:12:32 | +----+--------+--------------+-------+------+---------------------+ 2 rows in set (0.00 sec)二.向表中指定字段插入數(shù)據(jù)
- 向表中插入數(shù)據(jù)時(shí),也可以只指定一部分字段的值,語法:
INSERT INTO 表名(指定的字段1,指定的字段2,指定的字段3,...)VALUES(數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,...);實(shí)例3
題目:向goods表中指定字段插入數(shù)據(jù)。
1.向goods表中插入數(shù)據(jù)
mysql> INSERT INTO goods(type,name,price)-> VALUES('蔬菜','胡蘿卜',6); Query OK, 1 row affected (0.01 sec)2.查詢數(shù)據(jù)結(jié)果
mysql> SELECT*FROM goods; +----+--------+--------------+-------+------+---------------------+ | id | type | name | price | num | add_time | +----+--------+--------------+-------+------+---------------------+ | 1 | 書籍 | 斗羅大陸 | 38.50 | 30 | 2022-02-12 15:30:27 | | 2 | 水果 | 火龍果 | 15.00 | 213 | 2022-03-06 14:12:32 | | 3 | 蔬菜 | 胡蘿卜 | 6.00 | 0 | NULL | +----+--------+--------------+-------+------+---------------------+ 3 rows in set (0.00 sec)根據(jù)結(jié)果可得出:
當(dāng)字段沒有約束時(shí),系統(tǒng)會將字段設(shè)置為NULL。當(dāng)字段設(shè)置為自增約束時(shí),系統(tǒng)會將字段設(shè)置為自增后的順序。當(dāng)字段設(shè)置為默認(rèn)約束時(shí),系統(tǒng)會將字段設(shè)置為默認(rèn)值。三.同時(shí)插入多條數(shù)據(jù)
- 語法:
INSERT INTO 表名(字段1,字段2,字段3,...) VALUE(數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,...),... (數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,...);實(shí)例4
- 題目:向goods表 中插入多條數(shù)據(jù)。
1.選擇數(shù)據(jù)庫。
mysql> USE db_shop; Database changed2.向表中插入多條數(shù)據(jù)。
mysql> INSERT INTO goods(id,type,name,price,num,add_time)-> VALUES(4,'冷飲','百事可樂',3,6,'2022-04-03 15:12:34'),-> (5,'零食','QQ糖',1,10,'2022-03-01 12:30:56'),-> (6,'服裝','牛仔褲',65,4,'2022-06-05 16:32:20'); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0四.將其他表中的數(shù)據(jù)插入到表中
- 語法:
INSERT INTO 表名1(字段列表1)SELECT 字段列表2 FROM 表名2;實(shí)例5(1)
題目:創(chuàng)建new_goods表,并插入記錄。
1.創(chuàng)建new_goods表。
mysql> CREATE TABLE new_goods(-> id INT(11) PRIMARY KEY AUTO_INCREMENT,-> type VARCHAR(30) NOT NULL,-> name VARCHAR(30) UNIQUE,-> price DECIMAL(7,2) UNSIGNED,-> num INT(11) DEFAULT 0,-> add_time DATETIME-> ); Query OK, 0 rows affected, 3 warnings (0.12 sec)2.將goods表中的數(shù)據(jù)插入到new_goods表中。
mysql> INSERT INTO new_goods(id,type,name,price,num,add_time)-> SELECT id,type,name,price,num,add_time FROM goods; Query OK, 6 rows affected (0.01 sec) Records: 6 Duplicates: 0 Warnings: 03.查詢數(shù)據(jù)表。
mysql> SELECT*FROM new_goods; +----+--------+--------------+-------+------+---------------------+ | id | type | name | price | num | add_time | +----+--------+--------------+-------+------+---------------------+ | 1 | 書籍 | 斗羅大陸 | 38.50 | 30 | 2022-02-12 15:30:27 | | 2 | 水果 | 火龍果 | 15.00 | 213 | 2022-03-06 14:12:32 | | 3 | 蔬菜 | 胡蘿卜 | 6.00 | 0 | NULL | | 4 | 零食 | QQ糖 | 1.00 | 10 | 2022-03-01 12:30:56 | | 5 | 冷飲 | 百事可樂 | 3.00 | 6 | 2022-04-03 15:12:34 | | 6 | 服裝 | 牛仔褲 | 65.00 | 4 | 2022-06-05 16:32:20 | +----+--------+--------------+-------+------+---------------------+ 6 rows in set (0.00 sec)實(shí)例5(2)
題目:創(chuàng)建test_goods表,并插入數(shù)據(jù)。
1.選擇數(shù)據(jù)庫
mysql> USE db_shop; Database changed2.創(chuàng)建數(shù)據(jù)表
mysql> USE db_shop; Database changed mysql> CREATE TABLE test_goods(-> t_id INT(11) PRIMARY KEY AUTO_INCREMENT,-> t_name VARCHAR(30) UNIQUE,-> t_price DECIMAL(7,2) UNSIGNED-> ); Query OK, 0 rows affected, 2 warnings (0.10 sec)3.將goods表中的id,name,price字段插入到test_goods表中。
mysql> INSERT INTO test_goods(t_id,t_name,t_price)-> SELECT id,name,price FROM goods; Query OK, 6 rows affected (0.02 sec) Records: 6 Duplicates: 0 Warnings: 0總結(jié)
以上是生活随笔為你收集整理的数据表的基本操作(四)数据的插入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2003年高考语文全国最高分_2020年
- 下一篇: android小作业,Android s