数据库的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操作,其中查询在这里面比较重要,内容也较多,利用查询可以方便的从众多数据中查到我们想要的数据。
数据库的CRUD操作的更多相关文章
- Spring Boot使用Spring Data Jpa对MySQL数据库进行CRUD操作
只需两步!Eclipse+Maven快速构建第一个Spring Boot项目 构建了第一个Spring Boot项目. Spring Boot连接MySQL数据库 连接了MySQL数据库. 本文在之前 ...
- 4月18 数据库的CRUD操作
php主要是实现B/S Brower Server;此外还有C/S:Client Server暂时不考虑: LAMP: Linux系统 A阿帕奇服务器 Mysql数据库 Php语言,而现在学的是在wi ...
- Mybatis框架 使用接口Mapper实现数据库的crud操作
Mybatis的Mapper接口方式实现简单crud操作: 1.创建实体类 与数据库对应 我的实体类是<Student> package com.hxzy.mybatis.pojo; ...
- 完成对数据库的CRUD操作
PS:查询相对复杂,要处理结果集,增删改则不用. package it.cast.jdbc; import java.sql.Connection; import java.sql.ResultSet ...
- MyBatis 对数据库进行CRUD操作
1.update修改 uodate修改也可以使用之前的机制在配置文件中直接编写sql 但是update语句的set字句中是根据传入的值决定的, 此时可以通过Mybatis提供的标签实现判断动态拼接up ...
- mySQL CRUD操作(数据库的增删改查)
一.数据库操作 1.创建数据库 create database 数据库名称 2.删除数据库 drop database 数据库名称 二.表操作 1.创建表 create table 表名 ( ...
- 数据库CRUD操作以及MyBatis的配置使用
• 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发 • 业务字段设计 实体: name: ...
- javaweb学习总结(三十三)——使用JDBC对数据库进行CRUD
一.statement对象介绍 Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的exe ...
- Android下的SQLite数据库的相关操作及AndroidTestCase测试
一:创建数据库 package com.itcode.mysqlite; import android.content.Context; import android.database.sqlite. ...
随机推荐
- ZOJ3582:Back to the Past(概率DP)
Recently poet Mr. po encountered a serious problem, rumor said some of his early poems are written b ...
- insert into select * from 锁表
mysql[192.168.11.187] processid[249] root@localhost in db[zjzc] hold transaction time 197 112069858, ...
- Handler sendMessage 与 obtainMessage (sendToTarget)
这篇文章讲的很好: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 两种用法: 1. private void se ...
- 【CF】283D Tennis Game
枚举t加二分判断当前t是否可行,同时求出s.注意不能说|a[n]| <= |3-a[n]|就证明无解,开始就是wa在这儿了.可以简单想象成每当a[n]赢的时候,两人都打的难解难分(仅多赢一轮): ...
- JavaScript元素的创建、添加、删除
<script> var x=document.getElementById("p2"); var obj=document.createElement("p ...
- Hash Killer I II
题意大概: 1.字符串hash不取模,自动溢出 构造数据卡这种hash 2.字符串hash取模1000000007,构造数据卡这种hash 题解传送门:VFleaKing http://vfleak ...
- ☀【jQuery插件】DOM 延迟渲染
test.html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...
- 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 三)
原文网址:http://www.cnblogs.com/ChinaKingKong/p/4964745.html 前言部分 part三 部分主要讲解 Xcode关联绑定发布证书的配置.创建App信息. ...
- android学习—— LayoutInflater的使用
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(),不同点是LayoutInflater是 用来找layout下xml布局文件,并且实例化!而fi ...
- GridControl 列中显示图片 z
如何在 DevExpress.XtraGrid.GridControl 显示图片列. 方法很多,我把它们逐一写在附言中,方便大家分情况合理使用. 附言1 附言2 附言3 第 1 条附言 · ...