sql数据库增删改查的基本命令,sql增删改查语句怎么写
sql数据库增删改查的基本命令,sql增删改查语句怎么写详细介绍
本文目录一览: SQL常用增删改查语句
SQL常用增删改查语句大全
SQL常用的增删改查语句有哪些?为方便同学们复习SQL语句,我为大家分享SQL增删改查语句如下:
1增
1.1【插入单行】
insert [into]
(列名) values (列值)
例:insert into Strdents (姓名,性别,出生日期) values ('开心朋朋','男','1980/6/15')
1.2【将现有表数据添加到一个已有表】
insert into
(列名) select
from
例:insert into tongxunlu ('姓名','地址','电子邮件')
select name,address,email
from Strdents
1.3【直接拿现有表数据创建一个新表并填充】
select
into
from
例:select name,address,email into tongxunlu from strdents
1.4【使用union关键字合并数据进行插入多行】
insert
select
tnion select
例:insert Students (姓名,性别,出生日期)
select '开心朋朋','男','1980/6/15' union(union表示下一行)
select '蓝色小明','男','19**/**/**'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2删
2.1【删除
行】
delete from
[where
]
例:delete from a where name='开心朋朋'(删除表a中列值为开心朋朋的行)
2.2【删除整个表】
truncate table
truncate table tongxunlu
注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3改
update
set
[where
]
例:update tongxunlu set 年龄=18 where 姓名='蓝色小名'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4查
4.1``精确(条件)查询
select
from
[where
] [order by
[asc或desc]]
4.1.1【查询所有数据行和列】
例:select * from a
说明:查询a表中所有行和列
4.1.2【查询部分行列--条件查询】
例:select i,j,k from a where f=5
说明:查询表a中f=5的所有行,并显示i,j,k3列
4.1.3【在查询中使用AS更改列名】
例:select name as 姓名 from a where xingbie='男'
说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示
4.1.4【查询空行】
例:select name from a where email is null
说明:查询表a中email为空的所有行,并显示name列;SQL语句中用is null或者is not null来判断是否为空行
4.1.5【在查询中使用常量】
例:select name, '唐山' as 地址 from Student
说明:查询表a,显示name列,并添加地址列,其列值都为'唐山'
4.1.6【查询返回限制行数(关键字:top percent)】
例1:select top 6 name from a
说明:查询表a,显示列name的前6行,top为关键字
例2:select top 60 percent name from a
说明:查询表a,显示列name的60%,percent为关键字
4.1.7【查询排序(关键字:order by , asc , desc)】
例:select name
from a
where chengji>=60
order by desc
说明:查询a表中chengji大于等于60的所有行,并按降序显示name列;默认为ASC升序
4.2``模糊查询
4.2.1【使用like进行模糊查询】
注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用
例:select * from a where name like '赵%'
说明:查询显示表a中,name字段第一个字为赵的记录
4.2.2【使用between在某个范围内进行查询】
例:select * from a where nianling between 18 and 20
说明:查询显示表a中nianling在18到20之间的记录
4.2.3【使用in在列举值内进行查询】
例:select name from a where address in ('北京','上海','唐山')
说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段
4.3``.分组查询
4.3.1【使用group by进行分组查询】
例:select studentID as 学员编号,AVG(score) as 平均成绩 (注释:这里的score是列名)
from score (注释:这里的score是表名)
group by studentID
说明:在表score中查询,按strdentID字段分组,显示strdentID字段和score字段的平均值;select语句中只允许被分组的'列和为每个分组返回的一个值的表达式,例如用一个列名作为参数的聚合函数
4.3.2【使用having子句进行分组筛选】
例:select studentID as 学员编号,AVG(score) as 平均成绩 (注释:这里的score是列名)
from score (注释:这里的score是表名)
group by studentID
having count(score)>1
说明:接上面例子,显示分组后count(score)>1的行,由于where只能在没有分组时使用,分组后只能使用having来限制条件。
4.4``.多表联接查询
4.4.1内联接
4.4.1.1【在where子句中指定联接条件】
例:select a.name,b.chengji
from a,b
where a.name=b.name
说明:查询表a和表b中name字段相等的记录,并显示表a中的name字段和表b中的chengji字段
4.4.1.2【在from子句中使用join…on】
例:select a.name,b.chengji
from a inner join b
on (a.name=b.name)
说明:同上
4.4.2外联接
4.4.2.1【左外联接查询】
例:select s.name,c.courseID,c.score
from strdents as s
left outer join score as c
on s.scode=c.strdentID
说明:在strdents表和score表中查询满足on条件的行,条件为score表的strdentID与strdents表中的sconde相同
4.4.2.2【右外联接查询】
例:select s.name,c.courseID,c.score
from strdents as s
right outer join score as c
on s.scode=c.strdentID
说明:在strdents表和score表中查询满足on条件的行,条件为strdents表中的sconde与score表的strdentID相同
;
排序的列名>
查询条件表达试>
表名>
列名>
更新条件>
列名=更新值>
表名>
表名>
删除条件>
表名>
满足条件的>
列值>
列值>
列名>
表名>
源表名>
新建表名>
新建表列名>
原表名>
原表列名>
已有的新表>
表名>
sql数据库常用方法怎么增删改查
结构化查询语言(StructuredQueryLanguage)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。今天就给大家介绍数据库的基本SQL操作增删改查!!!材料/工具电脑SQLServer为表添加主键altertable主键添加前:主键添加后:插入数据insertinto查询查询全部记录:select*from条件查询(查询全部字段用*,查询指定字段也可以):select带有Sql函数的查询:selectcount(*)from模糊查询(like语法):select删除(Delete)deletefrom更新(update)update为表添加一列注意:列增加后不可删除。DB2中的列加上后数据类型也不能改变,唯一能改变的是增加varchar的长度。altertable
增删改查sql语句
1、数据库增加数据:
1)插入单行
insert [into]
(列名) values (列值)
例:insert into t_table (name,sex,birthday) values ('开心朋朋','男','1980/6/15')
2)将现有表数据添加到一个已有表 insert into
(列名) select
from
例:insert into t_table ('姓名','地址','电子邮件')
select name,address,email from t_table
3)直接拿现有表数据创建一个新表并填充 select
into
from
例:select name,address,email into t_table from strde
2、数据库删除数据:
1)删除
行delete from
[where
]。
例:delete from t_table where name='开心朋朋'(删除表t_table中列值为开心朋朋的行)
2)删除整个表 truncate table
truncate table tongxunlu
注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表
3、数据库修改数据 update
set
[where
]
例:update t_table set age=18 where name='蓝色小名'
4、数据库查询数据:
1)精确(条件)查询select
from
[where
] [order by
[asc或desc]]
2)查询所有数据行和列。例:select * from a
说明:查询a表中所有行和列
3)使用like进行模糊查询
注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用
例:select * from a where name like '赵%'
说明:查询显示表a中,name字段第一个字为赵的记录
4)使用between在某个范围内进行查询
例:select * from a where nianling between 18 and 20
说明:查询显示表a中nianling在18到20之间的记录
5)使用in在列举值内进行查询
例:select name from a where address in ('北京','上海','唐山')
说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段
扩展资料:
插入之前需要创建数据表,创建方式如下:
CREATE TABLE 表名称(列名称1 数据类型,列名称2 数据类型,列名称3 数据类型,....)
例如:--流程步骤定义表
create table T_flow_step_def(
Step_no int not null, --流程步骤ID
Step_name varchar(30) not null, --流程步骤名称
Step_des varchar(64) not null, --流程步骤描述
Limit_time int not null, --时限
URL varchar(64) not null, --二级菜单链接
Remark varchar(256) not null,
)
参考资料:百度百科-sql语句大全
sql语句最基本就是增删改查。
软件:sqlserver2005
电脑:WIN10
系统:ISO
1、点击management studio,连接到你的数据库。
2、在你要操作的数据库点击右键--新建查询。
3、插入语句,在右侧输入命令,执行,如图3。
4、删除语句,我们把刚刚添加的删除,在右侧输入命令,执行,如图4。
5、修改语句,在右侧输入命令,查询一下看是不是改了。
排序的列名>
查询条件表达试>
表名>
列名>
更新条件>
列名=更新值>
表名>
表名>
删除条件>
表名>
满足条件的>
源表名>
新建表名>
新建表列名>
原表名>
原表列名>
已有的新表>
表名>
用SQL语句随便写一条数据库增删改查语句
一、增:有2种方法
1.使用insert插入单行数据:
语法:insert [into]
[列名] values
例:insert into Strdents (姓名,性别,出生日期) values ('王伟华','男','1983/6/15')
注意:如果省略表名,将依次插入所有列
2.使用insert,select语句将现有表中的 数据添加到已有的新表中
语法:insert into
select
from
例:insert into addressList ('姓名','地址','电子邮件')select name,address,email
from Strdents
注意:查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致
二、删:有2中方法
1.使用delete删除数据某些数据
语法:delete from
[where
]
例:delete from a where name='王伟华'(删除表a中列值为王伟华的行)
注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名
2.使用truncate table 删除整个表的数据
语法:truncate table
例:truncate table addressList
注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能
用于有外建约束引用的表
三、改 使用update更新修改数据
语法:update
set
[where
]
例:update addressList set 年龄=18 where 姓名='王伟华'
注意:set后面可以紧随多个数据列的更新值(非数字要引号);where子句是可选的(非数字要引号),用来限制条件,如果不选则整个表的所有行都被更新
四、查
语法:select
from
[where
] [order by
<排序的列
名>[asc或desc]]
1).查询所有数据行和列
例:select * from a
说明:查询a表中所有行和
2).查询部分行列--条件查询
例:select i,j,k from a where f=5
说明:查询表a中f=5的所有行,并显示i,j,k3列
3).在查询中使用AS更改列名
例:select name as 姓名 from a where gender='男'
说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示
4).查询空行
例:select name from a where email is null
说明:查询表a中email为空的所有行,并显示name列;SQL语句中用is null或者is not null
来判断是否为空行
5).在查询中使用常量
例:select name '北京' as 地址 from a
说明:查询表a,显示name列,并添加地址列,其列值都为'北京'
6).查询返回限制行数(关键字:top )
例1:select top 6 name from a
说明:查询表a,显示列name的前6行,top为关键字(oracle 中没有top关键字
用rownum替代)
select * from a where rownum<6
7).查询排序(关键字:order by , asc , desc)
例:select name
from a
where grade>=60
order by desc
说明:查询表中成绩大于等于60的所有行,并按降序显示name列;默认为ASC升序
排序的列>
查询条件表达试>
表名>
列名>
更新条件>
列名=更新值>
表名>
表名>
删除条件>
表名>
原表名>
原表列名>
列名>
已有的新表>
列值>
表名>
sql语句的增删改查
1、数据库增加数据:
1)插入单行
insert [into]
(列名) values (列值)
例:insert into t_table (name,sex,birthday) values ('开心朋朋','男','1980/6/15')
2)将现有表数据添加到一个已有表 insert into
(列名) select
from
例:insert into t_table ('姓名','地址','电子邮件')
select name,address,email from t_table
3)直接拿现有表数据创建一个新表并填充 select
into
from
例:select name,address,email into t_table from strde
2、数据库删除数据:
1)删除
行delete from
[where
]。
例:delete from t_table where name='开心朋朋'(删除表t_table中列值为开心朋朋的行)
2)删除整个表 truncate table
truncate table tongxunlu
注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表
3、数据库修改数据 update
set
[where
]
例:update t_table set age=18 where name='蓝色小名'
4、数据库查询数据:
1)精确(条件)查询select
from
[where
] [order by
[asc或desc]]
2)查询所有数据行和列。例:select * from a
说明:查询a表中所有行和列
3)使用like进行模糊查询
注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用
例:select * from a where name like '赵%'
说明:查询显示表a中,name字段第一个字为赵的记录
4)使用between在某个范围内进行查询
例:select * from a where nianling between 18 and 20
说明:查询显示表a中nianling在18到20之间的记录
5)使用in在列举值内进行查询
例:select name from a where address in ('北京','上海','唐山')
说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段
扩展资料:
插入之前需要创建数据表,创建方式如下:
CREATE TABLE 表名称(列名称1 数据类型,列名称2 数据类型,列名称3 数据类型,....)
例如:--流程步骤定义表
create table T_flow_step_def(
Step_no int not null, --流程步骤ID
Step_name varchar(30) not null, --流程步骤名称
Step_des varchar(64) not null, --流程步骤描述
Limit_time int not null, --时限
URL varchar(64) not null, --二级菜单链接
Remark varchar(256) not null,
)
参考资料:百度百科-sql语句大全
4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、说明:删除新表
drop table tabname
6、说明:增加一个列
Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
7、说明:添加主键: Alter table tabname add primary key(col)
说明:删除主键: Alter table tabname drop primary key(col)
8、说明:创建索引:create [unique] index idxname on tabname(col….)
删除索引:drop index idxname on tabname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:创建视图:create view viewname as select statement
删除视图:drop view viewname
10、说明:几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1[separator]
添加:
insert
into
table(a,b)
vlaue(111,222);
删除:
delete
from
table
where
id=1;
改:
update
table
set
a
=
333
where
id=2;
查:
select
*
from
table
where
id=4;
下面教大家sql增删改查语句怎么写,操作方法如下。
1、首先在电脑中打开navicat,点击新建查询。
2、然后在打开的软件中,写出insert语句增加数据。
3、接着用delete语句删除数据。
4、最后写出update语句对数据进行修改,用select语句对数据进行查询,这样就完成了。
排序的列名>
查询条件表达试>
表名>
列名>
更新条件>
列名=更新值>
表名>
表名>
删除条件>
表名>
满足条件的>
源表名>
新建表名>
新建表列名>
原表名>
原表列名>
已有的新表>
表名>
SQl常用增删改查
SQl常用增删改查模板
篇一:SQl常用增删改查 SQL常用增删改查语句
增加
现在有一张表,表(Test)里面有三个字段,分别为sno,sname,age。举例用一条增加SQL语句,插入一条数据进库。
语句:
Insert into 表名 value(‘数据1’,’数据2’,’数据3’)
具体操作: Insert into testvalues('test','test','1')
通过上面这条语句,Test表里面就多了一条数据。如下图所示:
上面这个例子,是在每条字段都需要插入的时候为了方便而直接在 into 后面跟表名。但是也会遇到一些特殊的情况,比如一张表,因为有主外键约束(我这里只有一张表),而我只想插入被约束的字段sno(主键)加上age这个字段,在 into的时候就需要指明需要插入的字段,下面举例说明:
语句:
Insert into 表名(‘字段名1’,’字段名2’) values(‘数据1’,’数据2’)
具体操作:
into test(sno,age)values('彭宇','21')
这样数据库里面,又多了一条数据,而没有插入任何数据那个字段默认为NULL。如下图所示:
删除
在我们增加数据入库的时候,难免会出现数据录入错误,或者信息过期后不再需要的数据,所以我们要利用删除语句将表里面不需要的数据删除掉。下面举例说明。
语句:
Delete from 表名 where 字段名='需要删除的数据'
具体操作:
from test where sno='test'
通过这条SQL语句,Test表主键sno字段里面数据为test的该条数据就已经被删除了。
Ps:一般来说都以主键为条件进行删除,因为主键是不可重复的,我们可以设想一下,如果没使用主键为删除条件,假设一个公司有两个叫彭宇的人。我使用sname=’彭宇’作为删除条件的话,那么这两个同名同姓人的资料都会被删除掉,所以这是不可取的。
批量删除
当有多条数据需要删除的时候,我们可以使用批量删除语句来实现一次删除多条数据。
语句:
from表名where字段名in('该字段里面的数据1','该字段里面的数据2',……)
具体操作:
首先,看一下Test表里面有多少条数据,如下图:
现在我想利用一条SQL语句,将前三条数据删除掉。
from test where sno in('test','test2','test3')
通过执行这条SQL语句后,前三条数据已经被我批量删除了。
修改
一条已经录入数据库里面的数据如果需要更新、修正,我们就需要用到SQL修改语句。
语句:
Update 表名set字段='修改后的数据' where 字段='修改条件'
具体操作:
Update test set sno='SQL修改语句' where sno='test'
修改前后比较,下图所示: (
修改前
)(修改后)
查询
上面进行了增加,修改操作后,数据库里面已经存在有数据了,最后我们要利用SQL查询语句将它们查询并显示出来。
全部查询
语句:
Select * from 表名
具体操作:
Select * from test
执行了上面这句话,那么test表里面存在的数据都会被查询出来,如果我想要单独查询出某个人的数据怎么办?很简单,只需要加上一个关键词where就能够实现了。
单条件查询
语句:
Select * from 表名 where 字段=’需要查询的数据’
具体操作:
Select * from test where sno=’彭宇’
这样我就查询出数据库里面sno字段为彭宇的数据了。
多条件查询
多条件查询就是比起单条件查询多了一个and关键词,使用多条件查询,查出来的结构能够更加的精确。
语句:
Select * from 表名 where 字段=’需要查询的数据’ and 字段=’需要查询的数据’
具体操作:
Select * from test where sno=’彭宇’’21’ and age=
篇二:SQL语句增删改查 一、删:有2中方法
1.使用删除数据某些数据
语法: from
[where
]
例: from a where name='开心朋朋'(删除表a中列值为开心朋朋的行) 注意:删除整行不是删除单个字段,所以在后面不能出现字段名
2.使用truncate table 删除整个表的数据
语法:truncate table
例:truncate table tongxunlu
注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表
二、改
使用update更新修改数据
语法:
set
[where
]例:tongxunlu set 年龄=18 where 姓名='蓝色小名'
注意:set后面可以紧随多个数据列的更新值;where子句是可选的,用来限制条件,如果不选则整个表的所有行都被更新
四、查
1.普通查询
语法:select
from
[where
] [order by
[asc或desc]]
1).查询所有数据行和列
例:select * from a
说明:查询a表中所有行和列
2).查询部分行列--条件查询
例:select i,j,k from a where f=5 说明:查询表a中f=5的所有行,并显示i,j,k3列
3).在查询中使用AS更改列名
例:select name as 姓名 from a whrer xingbie='男'
说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示
4).查询空行
例:select name from a where email is null
说明:查询表a中email为空的所有行,并显示name列;SQL语句中用is null或者is not null来判断是否为空行
5).在查询中使用常量
例:select name '唐山' as 地址 from a
说明:查询表a,显示name列,并添加地址列,其列值都为'唐山'
6).查询返回限制行数(关键字:top percent)
例1:select top 6 name from a
说明:查询表a,显示列name的前6行,top为关键字
例2:select top 60 percent name from a
说明:查询表a,显示列name的60%,percent为关键字
7).查询排序(关键字:order by , asc , desc)
例:select name
from a
where chengji>=60
order by desc
说明:查询表中chengji大于等于60的所有行,并按降序显示name列;默认为ASC升序
2.模糊查询
1).使用like进行模糊查询
注意:like运算副只用语字符串,所以仅与char和varchar数据类型联合使用 例:select * from a where name like '赵%'
说明:查询显示表a中,name字段第一个字为赵的记录
2).使用between在某个范围内进行查询
例:select * from a where nianling between 18 and 20
说明:查询显示表a中nianling在18到20之间的记录
3).使用in在列举值内进行查询
例:select name from a where address in ('北京','上海','唐山')
说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段3.分组查询
1).使用group by进行分组查询
例:select studentID as 学员编号,AVG(score) as 平均成绩 (注释:这里的score是列名)
from score (注释:这里的score是表名)
group by studentID
说明:在表score中查询,按strdentID字段分组,显示strdentID字段和score字段的平均值;select语句中只允许被分组的列和为每个分组返回的一个值的表达试,例如用一个列名作为参数的聚合函数
2).使用having子句进行分组筛选
例:select studentID as 学员编号,AVG(score) as 平均成绩 (注释:这里的score是列名)
from score (注释:这里的score是表名)
group by studentID
having count(score)>1
说明:接上面例子,显示分组后count(score)>1的行,由于where只能在没有分组时使用,分组后只能使用having来限制条件,
4.多表联接查询
1).内联接
①在where子句中指定联接条件
例:select a.name,b.chengji
from a,b
where a.name=b.name
说明:查询表a和表b中name字段相等的记录,并显示表a中的name字段和表b中的chengji字段
②在from子句中使用join…on
例:select a.name,b.chengji
from a inner join b
on (a.name=b.name)
说明:同上
2).外联接
①左外联接查询
例:select s.name,c.courseID,c.score
from strdents as s
left outer join score as c
on s.scode=c.strdentID
说明:在strdents表和score表中查询满足on条件的行,条件为score表的.strdentID与strdents表中的sconde相同
②右外联接查询
例:select s.name,c.courseID,c.score
from strdents as s
right outer join score as c
on s.scode=c.strdentID
说明:在strdents表和score表中查询满足on条件的行,条件为strdents表中的sconde与score表的strdentID相同
三、增:有4种方法
1.使用插入单行数据:
语法: [into]
[列名] values
例: into Strdents (姓名,性别,出生日期) values ('开心朋朋','男','1980/6/15')
注意:into可以省略;列名列值用逗号分开;列值用单引号因上;如果省略表名,将依次插入所有列
2.使用 select语句将现有表中的数据添加到已有的新表中
语法: into
select
from
例: into tongxunlu ('姓名','地址','电子邮件')
select name,address,email
from Strdents
注意:into不可省略;查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致
3.使用select into语句将现有表中的数据添加到新建表中
语法:select
into
from
例:select name,address,email into tongxunlu from strdents
注意:新表是在执行查询语句的时候创建的,不能够预先存在
在新表中插入标识列(关键字‘identity’):
语法:select identity (数据类型,标识种子,标识增长量) AS 列名
into 新表 from 原表名
例:select identity(int,1,1) as 标识列,dengluid,password into tongxunlu from Struents
注意:关键字‘identity’
4.使用union关键字合并数据进行插入多行
语法:
select
tnion select
例: Students (姓名,性别,出生日期)
select '开心朋朋','男','1980/6/15' union(union表示下一行)
select '蓝色小明','男','19**/**/**'
注意:插入的列值必须和插入的列名个数、顺序、数据类型一致
篇三:SQL常用增删改查语句 SQLSQL常用增删改查语句
作者:hiker
一. Insert 插入语句
1. Insert into 表名(列名) values (对应列名值)//插入一行.
2. Insert into 新表名(列名)
Select (列名) 旧表名
3. Select 旧表名.字段…
Into 新表名 from 旧表名
4. Select identity ( 数据类型,标识种子,标识增长量) as 列名
Into新表名
From 旧表名
5. Insert 表名(列名)
Select (对应列名值) union
Select (对应列名值) union
Select (对应列名值)
二. Update 更新语句
1. Update 表名 set 列名=’更新值’ where 更新条件
三. 删除语句
1. from 表名 where 删除条件
2. truncate table 表名 //删除表中所有行
四. select 基本查询语句
1. select 列名 from 表名 where 查询条件
order by 排序的列名asc或desc升/降
2. select 列名 as 别名 from 表名 where 查询条件
3. select 列名 from 表名 where 列名 is null //查询空值
4. select 列名 , ‘常量值’ as 别名 from 表名//查询时定义输出一列常量值
5. select top 5 列名 from 表名 //查询前5行
6. select top 5 percent 列名 from 表名 //查询前百分之5的数据行
五.
1.
2.
3.
4. select 函数查询语句 selectLEN(Class_Name)fromClass //查询class_Name字符串长度 selectupper(Class_Name)fromClass //查询class_Name并转换为大写 ltrim和rtrim //清除字符串左右空格 selectREPLACE(card_No,'0','9')fromCardRecord//修改列中字符串中的字符 列名字符串中0修改为9
5. selectSTUFF(Card_No,2,3,'8888')fromCardRecord
列名字符串中第2个开始删除3个字符,再从第二个开始插入8888字符串
6. selectGETDATE()//显示系统日期
六.
1.
2.
3.
4.
5. select 高级查询语句 select * from 表名 where列名 like ‘ %s%’ //模糊查询 select * from 表名 where 列名 between 60 and 80 //范围查询 select * from 表名 where 列名 in (‘列举’,’’,’’) //在列举范围内查询 selectSUM(Score_Num)fromscores //查询分数总和 avg max min count //查询平均分/最大数/最小数/行数
selectcourse_Id,SUM(Score_Num)fromscores
groupbyCourse_Id//分组查询
havingCourse_Id='jsj001'//分组子句筛选
七. Select 多表连接查询语句
1.selects.stu_Nameas'姓名',c.Course_nameas'科目',sc.Score_Num
fromStudentsass
innerjoinScoresasscon(sc.Stu_Id=s.Stu_ID)
innerjoinCoursesascon(sc.Course_Id=c.Course_Id)
orderbys.Stu_Namedesc //三表内联查询
2.selects.stu_Nameas'姓名',c.Course_nameas'科目',sc.Score_Num
fromStudentsass
leftouterjoinScoresasscon(sc.Stu_Id=s.Stu_ID)
leftouterjoinCoursesascon(sc.Course_Id=c.Course_Id)
//三表左外联查询,以stu表为主,其它表为从。
3.selects.stu_Nameas'姓名',c.Course_nameas'科目',sc.Score_Num
fromCoursesasc
rightouterjoinScoresasscon(sc.Course_Id=c.Course_Id)
rightouterjoinStudentsasson(sc.Stu_Id=s.Stu_ID)
//三表右外联查询,以stu右表为主,其它表为从。
八. Create 创建数据库语句
1. create database 数据库名
on[primary]
(
[,…n] [
]
)
[log on]
(
{
[…n]}
)
文件参数:
Name=逻辑文件名,filename=物理文件名,size=大小,maxsize=最大容量,
Filegrowth=增长
文件组参数:
Filegroup 文件组名
例:
usemaster
go
ifexists(select*fromsysdatabaseswherename='abc')
dropdatabaseabc
createdatabaseabc
onprimary
(
name='abc',
filename='d:abc.mdf',
size=5,
maxsize=50,
filegrowth=10%
)
logon
(
name='abc_log',
filename='d:abc_log.ldf',
size=2,
maxsize=20,
filegrowth=1
)
2. use 数据库名
go
create table 表名
(
字段数据类型列的特征
)
Go
例:
usedb_myschool
go
ifexists(select*fromsysobjectswherename='test1')
droptabletest1
createtabletest1
(
Idintnotnull,
SNamenvar50)notnull,
Telintnotnull
)
go
3.使用SQL语句创建和删除约束
alter table表名
Add constraint 约束名约束类型描述说明
altertabledbo.testaddconstraintPK_IDprimarykey (ID)
主键:primary keyPK_ 唯一:uniqueUQ_ 检查:check CK_ 默认:defaultDF_外键:foreign keyFK_
1.execsp_addlogin'abc','abc'//添加SQL用户名
usedb_myqq
go
execsp_grantdbaccess'abc'//添加用户名到数据库中
3. 授权语句
Grant 权限 on 表名 to 数据库用户名 九. 登录验证语句
十. SQL编程语句
局部变量/全局变量
1.以@标记符作前缀
Declare @name var8)//声明
Set @name = value
Select @name=value//赋值
2.以@@标记符作前缀
@@error //最后一个T-SQL错误的错误号
@@identity //最后一次插入的标识值
@@language//当前使用的语言的名称
@@max_connections //可以创建的同时连接的最大数目
@@rowcount //受上一个SQL语句影响的行数
@@servername//本地服务器的名称
@@servicename //该计算机上的SQL服务的名称
@@timeticks //当前计算机上每刻度的微秒数
@@transcount //当前连接打开的事务数
@@version //SQL Server的版本信息
4. 输出
print'SQL服务名:'+@@servicename
select@@SERVICENAMEas'SQL服务名'
5. 逻辑控件语句
declare@avgfloat
select@avg=avg(Score_Num)fromScoreswhereStu_Id='sc0002'
print'平均分为'+convert(var8),@avg)+'分'
if(@avg>90)
begin
print'最高分'
selectMAX(Score_Num)fromScores
end
else
begin
print'最低分'
selectMIN(Score_Num)fromScores
6. while 循环语句
declare@nint
while(1=1)
begin
select@n=COUNT(*)fromScoreswhereScore_Num<60
if(@n>0)
updateScoressetScore_Num+=2 whereScore_Num<60
else
break
end
print'加分后的成绩'
select*fromScores
7. Case多分支语句
selectStu_id,score=case
whenScore_Num>90 then'A'
whenScore_Numbetween 80 and 89 then'B'
whenScore_Numbetween 60 and 79 then'C'
else'D'
end
fromScores
十一.高级查询
1. where子查询
2. in 和 not in 子查询
3. if exists (子查询)
;
文件参数>
日志文件参数>
文件参数>
数据文件参数>
列值>
列值>
列名>
表名>
源表名>
新建表名>
新建表列名>
原表名>
原表列名>
列名>
已有的新表>
列值>
表名>
排序的列名>
查询条件表达试>
表名>
列名>
更新条件>
列名=更新值>
表名>
表名>
删除条件>
表名>
sql增删改查语句怎么写
sql中的增删改查语句是用来对数据库中数据进行操作的,所以我们这篇文章就来看一下SQL增删查改语句的具体写法。一、SQL语句之增insert into 表的名字 (字段名)values(值); 向student表中插入一个学生的数据insert into student (num,name,sex,age)values(140010,张三,男,23)二、SQL语句之删删除student表中num=140011的这条数据。delete from student where num=140011;三、SQL语句之改我们可以将num为140010的age值更改为21。update student set age =21 where ID=140010;四、SQL语句之查查询语句非常的重要的,所以需要详细来说一下。1、查询student表中所有数据select * from student;2、查询student表中所有的name和sexselect name,sex from student;3、查询num为140010这一行的数据select * from where id =140010;
数据库中增删改查的基本语句是什么
常见如下:
进入mysql命令行: mysql -uroot -p;查看所有数据库: show databases;增加创建数据库: create database niu charset utf8;删除数据库: drop database niu;选择数据库: use databases。
查看所有表: show tables;查看创建数据库的语句:show create database databasename;查看创建表的语句:show create table tablename;查看表结构:desc tablenmae。
相关简介
mysql_stmt_fetch是函数名,mysql_stmt_fetch()返回结果集中的下一行。
仅能当结果集存在时调用它,也就是说,调用了能创建结果集的mysql_stmt_execute()之后,或当mysql_stmt_execute()对整个结果集即行缓冲处理后调用了mysql_stmt_store_result()。
使用mysql_stmt_bind_result()绑定的缓冲,mysql_stmt_fetch()返回行数据。对于当前列集合中的所有列,它将返回缓冲内的数据,并将长度返回到长度指针。
数据库增删改查的基本命令
以下是总结的mysql的常用语句,欢迎指正和补充~
一、创建库,删除库,使用库
1.创建数据库:create database 库名;
2.删除数据库:drop database 库名;
3.使用数据库:use 库名;
二、创建数据表
1.创建表语句:create table 表名(字段名1 字段类型 字段约束,字段2 字段类型 字段约束...);
2.创建与现有表一样字段的新表:create table 表名 like 已有表名;
3.将查询结果创建新表:create table 表名 select * from 现有表 where...(查询语句);
三、查看表结构,查看建表语句,删除表
1.查看表结构:desc 表名;
2.查看建表语句:show create table 表名;
3.删除表:drop table 表名;
四、修改表结构
1.对数据表重命名:alter table 表名 rename 新表名;
2.增加字段:alter table 表名 add 字段名 字段类型 字段约束; (PS:可用first/after函数调整字段位置)
3.删除字段:alter table 表名 drop 字段名;
4.修改字段类型及约束:alter table 表名 modify 字段名 新类型 新约束;(PS:如不加新约束,会将建表时的约束清空,主键、外键、唯一约束除外)
5.修改字段名称:alter table 表名 change 字段名 新字段名 新字段类型 新约束条件;
6.修改数据库引擎:alter table 表名 engine=;(PS:主要有InnoDB和MyISAM,InnoDB对经常修改表数据友好,MyISAM对经常查询表友好)
7.增加主键:alter table 表名 add primary key(字段名);
8.删除主键:alter table 表名 drop primary key;
9.增加外键:alter table 表名 add constraint 外键名 foreign kek(字段名) references 主表(主键);
10.删除外键:alter table 表名 drop foreign key 外键名;
11.删除唯一约束:alter table 表名 drop index 字段名;
12.设置自动增长的初始位置:alter table 表名 auto_increment=n;
五、向表中插入数据
1.向表指定字段插入多条数据:insert into 表名(字段1,字段2...) values(数据1,数据2...),(数据1,数据2...),(数据1,数据2...),(数据1,数据2...);
2.将查询结果插入表:insert into 表名 select 字段名 from 表名(查询语句);
3.加载外部数据到表:Load data local infile ‘数据路径’Into table 表名 Fields terminated by ‘分隔符’Ignored 1 lines;
六、更新表数据、删除表数据
1.更改满足条件的字段数据:update 表名 set 字段计算1,字段计算2... where 条件;
2.删除满足条件的数据:delele from 表名 where 条件;
3.删除所有数据:方式一:delete from 表名; 方式二:truncate table 表名; 方式一会逐条进行删除,速度较慢,方式二直接删除,速度快;另外对自增字段,方式一不能重置自增字段的初始位置,方式二可以重置自增字段的其实位置;