MySQL学习笔记:插入数据

本文为本人学习书籍《MySQL必知必会》笔记系列,欢迎持续关注,有问题随时留言评论,一起探讨学习~

19 插入数据

数据插入INSERT分为:

  1. 插入完整的行;
  2. 插入行的一部分;
  3. 插入多行;
  4. 插入某些查询结果

19.1插入完整的行

INSERT INTO customers(cust_name,
                      cust_address,
											cust_city,
											cust_state,
											cust_zip,
											cust_country,
											cust_contact,
											cust_email) 
VALUES('刘洋',
       '北京市朝阳区中国传媒大学',
			 '北京',
			 '北京',
			 '10010',
			 '中国',
			 '杨子',
			 '[email protected]');

19.2插入行的一部分:即只有部分列有值

INSERT INTO customers(cust_name,
											cust_city,
											cust_state,
											cust_country,
											cust_contact,
											cust_email) 
VALUES('李若寒',
			 '武汉',
			 '湖北',
			 '中国',
			 '涵涵',
			 '[email protected]');

19.3 插入多个行

INSERT INTO customers(cust_name,
											cust_country,
											cust_contact,
											cust_email) 
VALUES('赵梓明',
			 '中国',
			 'MM',
			 '[email protected]'),
			 ('刘露露',
			 '中国',
			 'LL',
			 '[email protected]');

19.4 插入检索结果INSERT SELECT

使用INSERT SELECT从custnew中将所有数据导入customers表

INSERT INTO customers(cust_id,
                      cust_name,
                      cust_address,
											cust_city,
											cust_state,
											cust_zip,
											cust_country,
											cust_contact,
											cust_email) 
SELECT id,
       cust_name,
       cust_address,
			 cust_city,
			 cust_state,
			 cust_zip,
			 cust_country,
			 cust_contact,
			 cust_email
FROM custnew;
发布了51 篇原创文章 · 获赞 34 · 访问量 880

猜你喜欢

转载自blog.csdn.net/weixin_43412569/article/details/104860847