数据库的CRUD操作

一:数据库的CRUD操作,C是指create新增,R是指retrieve检索,U是指update更改,D是指delete删除

SQL语句分为3类:

1.DDL指数据定义语言如:create,drop,alter等;

2.DML指数据操纵语言:CRUD;

3.DCL指数据控制语言:备份语言之类。

数据库类型分为3大类:

1.关系型数据库:用表存储数据,易于检索,冗余度较小,现在用的就是关系型数据库。

2.层次型数据库,不常见

3.网状型数据库,不常见

二:CRUD操作语法

首先创建一个数据库,一个fruit表,go是一个分割,如果不加go,一起执行语句会发生错误,创建了一个fruit表,ids列用primary key定义成了主键列,由于type和source是关键字,所以用一个[]括起来。

create database MyDB
go
use MyDB
go
create table fruit
(
ids int  primary key,
name varchar (20) not null,
price float,
[type] varchar(20),--type是关键词加[]
[source]varchar(20)

)
go

1.添加操作

这里ids没有设置为自增长列,用insert像表里添加内容,默认有几列()内就填几个值如第一句;如果不用默认的就自己指定哪一列填哪个值如第二第三句。

insert into fruit values('1','红富士','5','苹果','栖霞')
insert into fruit(ids,name,price,[type]) values('2','巨峰','6','葡萄')
insert into fruit(ids,name,[type],price,[source])values('3','冬枣','枣类','20','沾化')

如果ids设置为了自增长列,则第一列ids是不可以填入任何值的,默认就成了四列内容,如下面的语句

insert into fruit values('红香蕉','4.5','苹果','牟平')
insert into fruit values('黑美人','2.0','西瓜','淄博')
insert into fruit values('提子','10.0','葡萄','新疆')
insert into fruit values('肥桃','3.0','桃子','肥城')
insert into fruit values('鸭梨','5.0','梨','莱阳')
insert into fruit values('贵妃笑','20.0','荔枝','深圳')
insert into fruit values('莱阳梨','3.0','梨','莱阳')
insert into fruit values('红柚','5.0','梨','广西')
insert into fruit values('青青','16','火龙果','深圳')

完成之后的结果:

2.删除操作

delete删除慢,写日志,自增长往下继续不重新开始

delete from fruit

delete from fruit where 列名 关系运算符 值
delete from fruit where source ='莱阳'
delete from fruit where type='葡萄'  --去掉了产地是莱阳的行,去掉了类型是葡萄的行

条件多了也可以用and 或者 or,这样是删除了产地是深圳的价格小于18的行
delete from fruit where source='深圳'and price<18 

truncate table fruit --truncate快,截断不要,自增长列会从1开始复位

3.更新操作
--update fruit set 列名=值,列名=值,...where,如下语句:
update fruit set price=1000000 where name='青青'


update fruit set type='人类',source ='淄博' where name ='青青'

--begin tran和rollback,一个回滚操作,中间的句子操作错误了,可以用rollback返回,避免操作错误了无法挽回。
begin tran
update fruit set type='人类',source ='淄博' --这样就使得所有的行都改了,操作错误,可以用rollback返回操作前的状态。
rollback

4.查询操作

--对列的筛选叫做投影,对行的筛选叫做筛选
--行的名字叫记录或者元组,列的名字叫字段或者属性

查询操作并不改变数据库中的内容,只是将里面的数据按照需要的方式显示出来。

select * from fruit   -- *是代表所有的列

select name,type,source from fruit   --指定的列,这里指定了name列、type列和source列

select *from fruit where price>=5 and source='莱阳'   --指定行,这里指定了价格是大于等于5的产地是莱阳的这一行

select *from fruit where price between 10 and 20 --范围查询,查询价格在10到20之间的行
select *from fruit where price in(3,4,5) --离散值查询,查询价格是3,4,5的行

--列去重复
select distinct type from fruit --去除type类型中重复的项

--模糊查询

通配符:%是任意多个任意字符,_下划线代表一个任意字符,如%红%是指红的左右都可以有任意多个字符,需要写在单引号里。
select *from fruit where name like '%红%'   --查询name列中带有红字的


select *from fruit where price like '%5%'
select *from fruit where price like '_0%' or price like '__5%' --或者'_[3.5]%'[]内代表任取一个

--排序
select *from fruit order by price   --后面跟asc 可不用写,按照价格升序排列


select *from fruit order by price desc  --降序排列
select *from fruit order by price asc,ids desc  --先按照price升序排,price相同的按照ids降序排


select *from fruit where price>5 order by price  --先选出价格大于5的再按照价格升序排列

--统计,聚合函数
select COUNT(*)from fruit --查有多少条记录


select COUNT(*)from fruit where type like '梨'  --种类是梨的个数


select AVG(price) from fruit  --查平均值
select AVG(price) from fruit where type in('梨','苹果') --梨苹果的价格平均值


select SUM (price)from fruit --求和,()内可以是表达式如:price/ids


select *,(price*0.9) as 折后价格 from fruit  --在表右面又出线了一列“折后价格”显示的是9折之后的价格,仅显示,数据库内并没有存入。


select ids 序号,name 名称,price 价格,type 类型,source 产地 from fruit   --用汉字代替英文的列名


select MAX(price)from fruit  --查询价格最大值
elect Min(price)from fruit   --查询价格最小值

--分组一般配合统计函数用
select type,COUNT(*) from fruit group by type order by COUNT(*) desc --对分组后的数据进行排序,按照type(种类)进行分组,按照每组的个数降序排列,显示出type列和每组的个数


select type,COUNT(*)from fruit where price>5 group by type    --对价格大于5的按照type进行分组


select type,Min(price)from fruit group by type  --每个类型中价格最小值

select type,COUNT(*) from fruit group by type having COUNT(*)>1  --对分组后的数据进行筛选,having只能跟在group by后面用;按照type进行分组,将组内个数大于1的type和个数显示出来

以上就是一些基本的CRUD操作,其中查询在这里面比较重要,内容也较多,利用查询可以方便的从众多数据中查到我们想要的数据。

原文地址:https://www.cnblogs.com/Alvin-ftd/p/3956587.html