【MYSQL】insert into

  • insert into table-name values()
mysql> desc ns;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| uid   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| uname | varchar(20)      | NO   |     | NULL    |                |
| udate | date             | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> drop table ns;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into ns values(null,‘alex‘,now());
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> select * from ns;
+-----+-------+------------+
| uid | uname | udate      |
+-----+-------+------------+
|   1 | alex  | 2016-11-08 |
+-----+-------+------------+
1 row in set (0.00 sec)
  • 插入部分列
mysql> insert into ns values(null,‘alex‘);
ERROR 1136 (21S01): Column count doesn‘t match value count at row 1
mysql> insert into ns(uid,uname) values(null,‘alex‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from ns;
+-----+-------+------------+
| uid | uname | udate      |
+-----+-------+------------+
|   1 | alex  | 2016-11-08 |
|   2 | alex  | NULL       |
+-----+-------+------------+
2 rows in set (0.00 sec)
  • 插入非法null
mysql> insert into ns values(null,null,now());
ERROR 1048 (23000): Column ‘uname‘ cannot be null
  • 插入两行
mysql> insert into ns values(null,‘jat‘,19871117),(null,‘jet‘,14581122);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from ns;
+-----+-------+------------+
| uid | uname | udate      |
+-----+-------+------------+
|   1 | alex  | 2016-11-08 |
|   2 | alex  | NULL       |
|   3 | jat   | 1987-11-17 |
|   4 | jet   | 1458-11-22 |
+-----+-------+------------+
4 rows in set (0.00 sec)
  • 再次插入部分列
mysql> insert into ns(uname) values(‘james‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from ns;
+-----+-------+------------+
| uid | uname | udate      |
+-----+-------+------------+
|   1 | alex  | 2016-11-08 |
|   2 | alex  | NULL       |
|   3 | jat   | 1987-11-17 |
|   4 | jet   | 1458-11-22 |
|   5 | james | NULL       |
+-----+-------+------------+
5 rows in set (0.00 sec)
  • 使用set 插入数据
mysql> insert into ns set uname=‘lecake‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from ns;
+-----+--------+------------+
| uid | uname  | udate      |
+-----+--------+------------+
|   1 | alex   | 2016-11-08 |
|   2 | alex   | NULL       |
|   3 | jat    | 1987-11-17 |
|   4 | jet    | 1458-11-22 |
|   5 | james  | NULL       |
|   6 | lecake | NULL       |
+-----+--------+------------+
6 rows in set (0.00 sec)
  • column 列顺序与表不一致
mysql> insert into ns(uname,udate,uid) values (‘Kyrie‘,20160711,null)
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from ns;
+-----+--------+------------+
| uid | uname  | udate      |
+-----+--------+------------+
|   1 | alex   | 2016-11-08 |
|   2 | alex   | NULL       |
|   3 | jat    | 1987-11-17 |
|   4 | jet    | 1458-11-22 |
|   5 | james  | NULL       |
|   6 | lecake | NULL       |
|   7 | Kyrie  | 2016-07-11 |
+-----+--------+------------+
7 rows in set (0.00 sec)
  • 使用INSERT…SELECT语句插入从其他表选择的行
  •  查询不能包含一个ORDER BY子句,而且INSERT语句的目的表不能出现在SELECT查询部分的FROM子句.
mysql> select * from ns2;
+-----+--------+-----------+------------+
| uid | uname  | uemail    | udate      |
+-----+--------+-----------+------------+
|   1 | config | [email protected] | 1987-11-17 |
|   2 | enable | [email protected]  | 1458-11-22 |
+-----+--------+-----------+------------+
2 rows in set (0.00 sec)

mysql> select uname,udate from ns2;
+--------+------------+
| uname  | udate      |
+--------+------------+
| config | 1987-11-17 |
| enable | 1458-11-22 |
+--------+------------+
2 rows in set (0.00 sec)

mysql> insert into ns(uname,udate) select uname,udate from ns2;
Query OK, 2 rows affected (0.05 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from ns;
+-----+--------+------------+
| uid | uname  | udate      |
+-----+--------+------------+
|   1 | alex   | 2016-11-08 |
|   2 | alex   | NULL       |
|   3 | jat    | 1987-11-17 |
|   4 | jet    | 1458-11-22 |
|   5 | james  | NULL       |
|   6 | lecake | NULL       |
|   7 | Kyrie  | 2016-07-11 |
|   8 | config | 1987-11-17 |
|   9 | enable | 1458-11-22 |
+-----+--------+------------+
9 rows in set (0.00 sec)

mysql> 


INSERT 语法

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    VALUES ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
或:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    SET col_name={expr | DEFAULT}, ...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
或:

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    SELECT ...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
时间: 2024-10-24 18:31:23

【MYSQL】insert into的相关文章

FluentData -Micro ORM with a fluent API that makes it simple to query a database 【MYSQL】

官方地址:http://fluentdata.codeplex.com/documentation MYSQL: MySQL through the MySQL Connector .NET driver. 连接字符串:Server=127.0.0.1;Database=testDB;Uid=root;Pwd=jnex;<system.data> <DbProviderFactories> <add name="MySQL Data Provider" i

【MYSQL】Mysql 设备选型与系统规划-实战参考文档-带附件-可下载

更多精彩内容尽在www.leonarding.com <Mysql 设备选型与系统规划> 引言:项目的设备选型是一项细腻而又艰巨的任务.因为它有一次性.不可更改.兼容性.扩展性.功能性.性能性.易用性等参数阀值来参照.例如我们的项目,首先要了解今年的设备采购预算有多少,如何分配的,分配下来每个项目大概有多少,按照预算标准进行分级.其次由于我们是金融企业采购范围是有所限制的,会有一个备选目录,上面列举了品牌.厂家.规格等信息,只能在这些目录中选择,如果设备有超出目录的需要写说明申请. 再次就需要

【MySQL】mysql buffer pool结构分析

转自:http://blog.csdn.net/wyzxg/article/details/7700394 其他参考: <高性能MySQL> - 8.4.5 InnoDB缓冲池 <MySQL技术内幕InnoDB存储引擎>(第二版内容有所更新) - 2.3 InnoDB体系结构 ############################################## 书中是先对后台线程进行说明,然后对内存部分进行说明,这样更好理解innoDB引擎内存池在使用时的过程. [后台线程

【Mysql】常用指令之——用户操作(创建,授权,修改,删除)

Mysql中的用户 user 每一个user都对应了不同的用户地址和权限 创建Mysql用户共有三种方式1.create user 2.grant 3.操作mysql.user表 1.CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'aa'@'localhost' IDENTIFIED BY '123456'; CREATE USER 'aa'@'192.168.1.101_' IDENDIFIED

【Mysql】select

描述 The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. 大意:select用来从数据库中挑选数据 有以下两种形式 SELECT column_name,column_name FROM table_name; 和 SELECT * FROM table_name; 下面是测试代码 #include <

【MySQL】MySQL事务回滚脚本

MySQL自己的 mysqlbinlog | mysql 回滚不好用,自己写个简单脚本试试: 想法是用mysqlbinlog把需要回滚的事务区域从mysql-bin.file中找到,然后通过脚本再插入DB. ## INSERT 需要将新增数据删除 对应DELETE ## DELETE 需要将删除数据恢复 对应INSERT ## UPDATE 需要将修改数据恢复 对应UPDATE ## 手动读取BINLOG,并找到对应位置和对应事务 ## 手动删除除事务外的其他说明语句 INSERT回滚最简单,其

【MySql】赶集网mysql开发36条军规

[MySql]赶集网mysql开发36条军规 2012-05-14 14:02:33 分类: Linux 写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒过. (一)核心军规 (1)不在数据库做运算 cpu计算务必移至业务层: (2)控制单表数据量 int型不超过1000w,含char则不超过500w: 合理分表: 限制单库表数量在300以内: (3)控制列数量 字段少而精,字段数建议在20以内: (4)平衡范式与冗余 效率优先: 往往牺牲范式: (5)

【MySQL】【ProxySQL】浅析mysql_users表

[MySQL][ProxySQL]浅析mysql_users表 1.表定义与字段说明 表的DDL定义: CREATE TABLE mysql_users ( username VARCHAR NOT NULL, #用户名 password VARCHAR, #密码 active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1, #是否启用 use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0,

【MySQL】Unknown column &#39;column_name&#39; in &#39;field list&#39;

使用 INSERT INTO - SELECT FROM - ON DUPLICATE KEY UPDATE 时遇到了这个问题,百思不得其解?? 后来总算找到了解决方法,使用子查询.如下: 参考链接:https://stackoverflow.com/questions/2472229/insert-into-select-from-on-duplicate-key-update [MySQL]Unknown column 'column_name' in 'field list' 原文地址:h