MySQL中insert into语句的6种写法

MySQL中insert into语句的6种写法,第1张

insert into是mysql中最常用的插入语句,它有6种写法。

如果插入的记录是数字的话要在数字的逗号后面加n:

通过以上实例我们可以看到insert into语句只能向原表中插入于其字段对应的数据,那么能不能通过insert into语句来把其他表的数据插入到原表中呢:

在MySQL中set方法:

ModifyStatement.Set Method修改语句 set方法

Sets key and value. 设置键和值。

由于insert into语句是一个插入性的语句,所以它的功能要么向指定的表插入数据

也许你看到这个SQL语句是正确的,就觉得这样应该也可以:

mysql>mysql>insert into 4a set sname=4ainall.sname

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql>insert into 4a set sname=4ainall.sname' at line 1

或者这样也可以:

mysql>mysql>insert into 4a set sname="赵六"

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql>insert into 4a set sname="赵六"' at line 1

然后这样也是不可用:

mysql>insert into 4a select * from 4ainall set sname=4ainall.sname

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'from 4ainall set sname=4ainall.sname' at line 1

可以看出由于select是作用于4inall这个表的,而set方法也只能在select语句中,这就直接导致set方法只能作用于4inall这个表,而无法作用于4a这个表。

但是如果我们不用select语句的话编译器又怎么会知道4inall表中的数据在哪里?

显然select是用于查的而set则是一个用于改的方法,两者无法结合在一起——insert into set语句当然也不能用于将其他表的数据插入到原表中了。

在建立表的时候设置id为自动增长的 [id] [int] IDENTITY (1, 1)

SQL语句是insert into  user(name,passwd) values (name  ,passwd)。新增一条数据 id 就会自动加1

INSERT INTO是sql数据库中的语句,可以用于向表格中插入新的行。

扩展资料

(1) 数据记录筛选:

sql="select * from 数据表 where字段名=字段值 order by字段名[desc]"(按某个字段值降序排列。默认升序ASC)

sql="select * from 数据表 where字段名like '%字段值%' order by 字段名 [desc]"

sql="select top 10 * from 数据表 where字段名=字段值 order by 字段名 [desc]"

sql="select top 10 * from 数据表 order by 字段名 [desc]"

sql="select * from 数据表 where字段名in ('值1','值2','值3')"

sql="select * from 数据表 where字段名between 值1 and 值2"

(2) 更新数据记录:

sql="update 数据表 set字段名=字段值 where 条件表达式"

sql="update 数据表 set 字段1=值1,字段2=值2 ?? 字段n=值n where 条件表达式"

(3) 删除数据记录:

sql="delete from 数据表 where 条件表达式"

sql="delete from 数据表" (将数据表所有记录删除)

(4) 添加数据记录:

sql="insert into 数据表 (字段1,字段2,字段3 ?) values (值1,值2,值3 ?)"

sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表)

(5) 数据记录统计函数:

AVG(字段名) 得出一个表格栏平均值

COUNT(*字段名) 对数据行数的统计或对某一栏有值的数据行数统计

MAX(字段名) 取得一个表格栏最大的值

MIN(字段名) 取得一个表格栏最小的值

SUM(字段名) 把数据栏的值相加

引用以上函数的方法:

sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"

set rs=conn.excute(sql)

用 rs("别名") 获取统计的值,其它函数运用同上。

查询去除重复值:select distinct * from table1

(6) 数据表的建立和删除:

CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) ?? )

(7) 单列求和:

SELECT SUM(字段名) FROM 数据表

参考资料——百度百科SQL insert into

在mysql中要向数据库中保存数据我们最常用的一种方法就是直接使用Insert into语句来实现了,下面我来给大家详细介绍Insert into语句用法

INSERT用于向一个已有的表中插入新行。INSERT…VALUES语句根据明确指定的值插入行。让我们先来看一下insert语句标准的定义,放在[]内的都是可以省略的:

语法

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]

[INTO] tbl_name [(col_name,...)]

VALUES ({expr | DEFAULT},...),(...),...

[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

实例

create table links (name varchar(255) not null default '', address varchar(255) not null default '')

最简单的插入方法

代码如下

复制代码

Mysql>insert into worker values(‘tom’,’[email protected]’),(‘paul’,’[email protected]’)

insert into links values('jerichen','gdsz')


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5902563.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-07
下一篇 2023-03-07

发表评论

登录后才能评论

评论列表(0条)

保存