Mysql入门

1 SQL-DML基础

  • SQL(Structured Query Language)结构化查询语言
    • DML:数据操纵语言(使用者的角度,80%左右)
    • DDL:数据定义语言(建设者的角度,15%左右)
    • DCL:数据控制语言(管理员的角度,5%左右)

1.1 DML-insert

# 语法
# Insert into TableName
# (列1,列2.... 列n)
# Values
# (值1,值2,....值n)
# =============================================
# 新建一张表
create table user(name varchar(10),gender char(1),com varchar(20),salary int) 
charset utf8;
# 插入部分列
insert into user(name,com,salary) values('zhangsan','baidu',10000);
# 插入所有列
insert into user values('lisi','女','sina',8000);

1.2 DML-update

# 语法
# Update 表名 
# Set 列1 = 新值 1,列2 = 新值2,列n = 新值n.....
# Where  expr
# =============================================
# 修改zhangsan的公司为google
update user set com = 'google' where name = 'zhangsan';

1.3 DML-delete

# 语法
# Delete from 表名 where  expr
# =============================================
delete from user where name = 'lisi';

1.4 DML-select

# 语法
# Select1, 列2, 列3,...列n
# From 表名
# Where expr;
# =============================================
# 查询所有数据
select * from user;
# 查询某些列的数据
select name,com,salary from user;
# 查询某些列某些行
select name,com,salary from user where name = 'zhangsan';

2 select查询模型

  • 列是变量,可以参与运算

    有如下表和数组,把num值处于[20,29]之间,改为20,num值处于[30,39]之间的,改为30
    mian表
    +------+
    | num  |
    +------+
    |   3 |
    |   12 |
    |   15 |
    |   25 |
    |   23 |
    |   29 |
    |   34 |
    |   37 |
    |   32 |
    |   45 |
    |   48 |
    |   52 |
    +------+
    update mian set num = floor(num/10) * 10 where num between 20 and 39;
  • where后面接的是表达式(真假)

    where 1 = 1;  where 1;
  • 奇怪的NULL

    is null或者is not null判断是否为空值

3 综合练习

# 商品表:goods
# goods_id --主键,
# goods_name -- 商品名称
# cat_id  -- 栏目id
# brand_id -- 品牌id
# goods_sn -- 货号
# goods_number -- 库存量
# shop_price  -- 价格
# goods_desc --商品详细描述
# =============================================
# 栏目表:category
# cat_id --主键 
# cat_name -- 栏目名称
# parent_id -- 栏目的父id

3.1 基础查询where

1.1:主键为32的商品
select goods_id,goods_name,shop_price from goods where goods_id=32;

1.2:不属第3栏目的所有商品
select goods_id,cat_id,goods_name,shop_price from goods where cat_id!=3;

1.3:本店价格高于3000元的商品
select goods_id,cat_id,goods_name,shop_price  from goods where shop_price >3000;

1.4:本店价格低于或等于100元的商品
select goods_id,cat_id,goods_name,shop_price  from goods where shop_price <=100;

1.5:取出第4栏目或第11栏目的商品(不许用or)
select goods_id,cat_id,goods_name,shop_price  from goods 
    where cat_id in (4,11);

1.6:取出100<=价格<=500的商品(不许用and)
select goods_id,cat_id,goods_name,shop_price  from goods
     where shop_price between 100 and 500;

1.7:取出不属于第3栏目且不属于第11栏目的商品(and,或not in分别实现)
select goods_id,cat_id,goods_name,shop_price  from goods     
    where cat_id!=3 and cat_id!=11;
select goods_id,cat_id,goods_name,shop_price  from goods     
    where cat_id not in (3,11);

1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品()
select goods_id,cat_id,goods_name,shop_price from goods 
    where shop_price>100 and shop_price <300 
        or shop_price >4000 and shop_price <5000;

1.9:取出第3个栏目下面价格<1000或>3000,并且点击量>5的系列商品
select goods_id,cat_id,goods_name,shop_price,click_count from goods 
    where cat_id=3 and (shop_price <1000 or shop_price>3000) 
        and click_count>5;

1.10:取出第1个栏目下面的商品(注意:1栏目下面没商品,但其子栏目下有)
select goods_id,cat_id,goods_name,shop_price,click_count from goods
     where cat_id in (2,3,4,5);

1.11:取出名字以"诺基亚"开头的商品
select goods_id,cat_id,goods_name,shop_price from goods    
    where goods_name like '诺基亚%';

1.12:取出名字为"诺基亚Nxx"的手机
select goods_id,cat_id,goods_name,shop_price from goods  
   where goods_name like '诺基亚N__';

1.13:取出名字不以"诺基亚"开头的商品
select goods_id,cat_id,goods_name,shop_price from goods
     where goods_name not like '诺基亚%';

1.14:取出第3个栏目下面价格在10003000之间,并且点击量>5 "诺基亚"开头的系列商品
select goods_id,cat_id,goods_name,shop_price from goods 
    where cat_id=3 and shop_price>1000 
        and shop_price <3000 and click_count>5 and goods_name like '诺基亚%';
select goods_id,cat_id,goods_name,shop_price from goods 
    where shop_price between 1000 and 3000 and cat_id=3  
        and click_count>5 and goods_name like '诺基亚%';

3.2 group/having/order by/limit

分组函数中常用聚合函数
2.1:查出最贵的商品的价格
select max(shop_price) from goods;

2.2:查出最大(最新)的商品编号
select max(goods_id) from goods;

2.3:查出最便宜的商品的价格
select min(shop_price) from goods;

2.4:查出最旧(最小)的商品编号
select min(goods_id) from goods;

2.5:查询该店所有商品的库存总量
select sum(goods_number) from goods;

2.6:查询所有商品的平均价
 select avg(shop_price) from goods;

2.7:查询该店一共有多少种商品
 select count(*) from goods;
having与group by综合查询
3.1:查询该店的商品比市场价所节省的价格
select goods_id,goods_name,market_price-shop_price as j from goods ;

3.2:查询每个商品所积压的货款(提示:库存*单价)
select goods_id,goods_name,goods_number*shop_price from goods

3.3:查询该店积压的总货款
select sum(goods_number*shop_price) from goods;

3.4:查询该店每个栏目下面积压的货款.
select cat_id,sum(goods_number*shop_price) as k from goods group by cat_id;

3.5:查询比市场价省钱200元以上的商品及该商品所省的钱(where和having分别实现)
select goods_id,goods_name,market_price-shop_price as k from goods
    where market_price-shop_price >200;
select goods_id,goods_name,market_price-shop_price as k from goods
    having k >200;

3.6:查询积压货款超过2W元的栏目,以及该栏目积压的货款
select cat_id,sum(goods_number*shop_price) as k from goods 
    group by cat_id having k>20000

3.7:where-having-group综合练习题
# 有如下表及数据
# +------+---------+-------+
# | name | subject | score |
# +------+---------+-------+
# | 张三 | 数学    |    90 |
# | 张三 | 语文    |    50 |
# | 张三 | 地理    |    40 |
# | 李四 | 语文    |    55 |
# | 李四 | 政治    |    45 |
# | 王五 | 政治    |    30 |
# +------+---------+-------+
# 要求:查询出2门及2门以上不及格者的平均成绩
# 这里使用sum而不是count
select name,sum(score<60) as k,avg(score) from result group by name having k>=2;
order by 与 limit查询
4.1:按价格由高到低排序
select goods_id,goods_name,shop_price from goods order by shop_price desc;

4.2:按发布时间由早到晚排序
select goods_id,goods_name,add_time from goods order by add_time;

4.3:接栏目由低到高排序,栏目内部按价格由高到低排序
select goods_id,cat_id,goods_name,shop_price from goods
     order by cat_id ,shop_price desc;

4.4:取出价格最高的前三名商品
select goods_id,goods_name,shop_price from goods 
    order by shop_price desc limit 3;

4.5:取出点击量前三名到前5名的商品
select goods_id,goods_name,click_count from goods 
    order by click_count desc limit 2,3;

3.3 子查询

where子查询
# 查询每个栏目下最新商品的信息(goods_id最大为最新)
select goods_id,cat_id,goods_name from goods 
    where goods_id in (select max(goods_id) from goods group by cat_id);
from子查询
select cat_id,goods_id,goods_name from 
    (select * from goods order by cat_id asc,goods_id desc) as tmp 
    group by cat_id;
exists子查询
# 查出所有有商品的栏目
select * from category
    where exists (select * from goods where goods.cat_id=category.cat_id);

3.4 连接查询

左连接/右连接
# 取出所有商品的商品名,栏目名,价格
select goods_name,cat_name,shop_price from 
    goods left join category
    on goods.cat_id = category.cat_id;
# 右连接与左连接类似
# inner join或者join为等值连接
面试题
# 根据给出的表结构按要求写出SQL语句。
# Match 赛程表
# 字段名称      字段类型        描述
# matchID       int             主键
# hostTeamID    int             主队的ID
# guestTeamID   int             客队的ID
# matchResult   varchar(20)     比赛结果,如(2:0)
# matchTime     date            比赛开始时间
# =============================================
# Team  参赛队伍表
# 字段名称      字段类型        描述
# teamID        int             主键
# teamName      varchar(20)     队伍名称
# =============================================
# Match的hostTeamID与guestTeamID都与Team中的teamID关联
# 查出 2006-6-1 到2006-7-1之间举行的所有比赛,并且用以下形式列出:
# 拜仁  2:0 不来梅 2006-6-21

# select * from m;                                select * from t;
# +-----+------+------+------+------------+         +------+----------+
# | mid | hid  | gid  | mres | matime     |         | tid  | tname    |
# +-----+------+------+------+------------+         +------+----------+
# |   1 |    1 |    2 | 2:0  | 2006-05-21 |         |    1 | 国安     |
# |   2 |    2 |    3 | 1:2  | 2006-06-21 |         |    2 | 申花     |
# |   3 |    3 |    1 | 2:5  | 2006-06-25 |         |    3 | 公益联队 |
# |   4 |    2 |    1 | 3:2  | 2006-07-21 |         +------+----------+
# +-----+------+------+------+------------+
select hid,t1.tname as hname,mres,gid,t2.tname as gname,matime
    from m left join t as t1
        on m.hid = t1.tid
        left join t as t2
        on m.gid = t2.tid;

3.5 union查询

  • 列数一定要相同才可以进行union操作
  • union(自动去重,比较耗时,一般不让union进行合并)和union all
  • union的字句中,不需要写order by,总的结果集可以进行order by

4 SQL-DDL基础

4.1 数据类型

数值型
# 整型,默认为带符号的
# [TINYINT|SMALLINT|MEDIUMINT|INT|BIGINT] unsigned
# [TINYINT(M)|SMALLINT(M)|MEDIUMINT(M)|INT(M)|BIGINT(M)] zerofill
# unsigned指定不带符号
# zerofill适合用于学号,编码等固定宽度的数字,可以用0填充至固定宽度,
# 并且已经决定列为unsigned,M表示填充至多宽
TINYINT         无符号:0~255          有符号:-128~127
SMALLINT        无符号:0~65535            有符号:-32768~32767
MEDIUMINT       无符号:0~16777215     有符号:-8388608~8388607
INT             无符号:0~40多亿
BIGINT          无符号:0~18446744073709551615

# 浮点型
FLOAT[(M,D)][UNSIGNED][ZEROFILL]    M表示精度,总位数;D表示小数点位数
DOUBLE[(M,D)][UNSIGNED][ZEROFILL]
DECIMAL[(M,D)]      整数与小数分开存储,更精确
字符串型
# 字符串长度较小的话用CHAR,字符较长的话用VARCHAR
CHAR        
VARCHAR     
TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT
TINYBLOB|BLOB|MEDIUMBLOB|LONGBLOB
ENUM
# CHAR右侧补空格进行存储,取出时将右侧空格去除,如果存储的字符串右侧有空格则会丢失
时间日期型
DATE        日期(yyyy-mm-dd)                      1000-01-01~9999-12-31
TIME        时间(hh:mm:ss)     
DATETIME    日期与时间組合(yyyy-mm-dd hh:mm:ss)     
TIMESTAMP   yyyymmddhhmmss   
YEAR        年份yyyy                              1901~2155
列的默认值
create table t_column(id int not null default 0);

4.2 建表语句

create table 表名 (
列1 列类型[ 列属性 默认值],
列2 列类型 [列属性 默认值],
.....
列n列类型 [ 列属性 默认值]
);
engine = 存储引擎 
charset = 字符集
# 主键与自增
create table t_pri(id int not null primary key auto_increment);

4.3 列的增删改

# 添加列
alter table t_column add column name char(20);
# 修改列(change既可以修改数据类型也可以修改列名,modify只能修改数据类型)
alter table t_column change name sname varchar(20);
alter table t_column modify sname varchar(30);
# 删除一列
alter table t_column drop sname;

4.4 视图

# view又被称作虚拟表,view是sql的查询结果
# 1、用作权限控制,允许用户查询某几列
# 2、简化复杂的查询
# 3、视图是否可以更新、删除?
#   如果视图的每一行是与物理表一一对应的,则可以;若视图是由物理表经过计算得到的,则不可以
# 创建视图
create view v1 as select ...
# 视图存放在哪?(视图的algorithm)
# merge(合并)===========>合并创建视图的语句和对视图的查询语句
# temptable(临时表)===========>如果视图的语句本身比较复杂,很难和查询视图的语句合并,
# mysql可以先查询视图的创建语句,把结果集形成内存中的临时表,然后再去查临时表
create [algorithm = [merge|undefined|temptable]] view v2 as select ...

4.5 表/视图的管理语句

# 查看所有表
show tables;
# 查看表结构
desc 表名/视图名;
# 查看建表过程
show create table 表名;
# 查看表详细信息
show table status [where name = 表名] \G;
# 删除表、删除视图
drop table 表名   drop view 视图名;
# 修改表名
rename table 表名 to 新表名;
# 清空表数据
# truncatedelete from不一样,前者是删除表后建表,后者只删除数据
truncate table 表名;

4.6 存储引擎的概念

# 1、MyISAM数据库表文件:
# .MYD文件:即MY Data,表数据文件
# .MYI文件:即MY Index,索引文件
# .log文件:日志文件
# ============InnoDB在5.5之后为默认存储引擎===================================
# 2、InnoDB采用表空间(tablespace)来管理数据,存储表数据和索引,
# InnoDB数据库文件(即InnoDB文件集,ib-file set):
# ibdata1、ibdata2等:系统表空间文件存储InnoDB系统信息和用户数据库表数据和索引,所有表共用
# .ibd文件:单表表空间文件,每个表使用一个表空间文件(file per table),存放用户数据库表数据和索引
# 日志文件: ib_logfile1、ib_logfile2

# 文章,新闻等安全性要求不高的,选myisam
# 订单,资金,账单,火车票等对安全性要求高的,可以选用innodb
# 对于临时中转表,可以用memory型 ,速度最快

4.7 字符集与乱码问题

# 1:节省空间
# 建议在能够完全满足应用的前提下,尽量使用小的字符集。因为更小的字符集意味着能够节省空间、减少网络传输字节数,同时由于存储空间的较小间接的提高了系统的性能。有很多字符集可以保存汉字,比如 utf8、gb2312、gbk、gb18030 等等,但是常用的是gb2312 和 gbk。
# 2:兼容性
# 因为 gb2312 字库比 gbk 字库小,有些偏僻字(例如:洺)不能保存,因此在选择字符集的时候一定要权衡这些偏僻字在应用出现的几率以及造成的影响,
# 3:在互联网上,国际化的趋势不可避免,且存储空间已经越来海量化,因此推荐用utf8,如果开发内网系统,如内部OA等,可以考虑GBK。

4.8 索引

# 建表时直接声明索引:
create table tableName (
    列1 列类型 列属性,
    ....
    列N 列类型 列属性,
    primary key(列名),
    index [索引名](列名),
    unique[索引名](列名),
    # 中文环境下全文索引无效,一般用第三方解决方案
    fulltext[索引名](列名)
)engine=InnoDB ,charset=utf8;
# ===============================================
# 通过修改表建立索引
alter table add index [索引名](列名);
alter table add unique [索引名](列名);
alter table add primary key(列名);
alter table add fulltext [索引名](列名); 
索引优化
# 1、索引长度,N为建索引的长度
index [索引名](列名(N))
# 2、多列索引,把多列看成一个整体建立索引
index [索引名](列名1,列名2,...,列名N)
# 3、冗余索引,在某列上可能存在多个索引
索引操作
# 注:索引名一般是列名,如果不是,可通过show index from tableName查看索引
show index from 表名 \G
# ===============================================
# 删除主键:
alter table drop primary key
# 删除其他索引:
alter table drop index 索引名
drop index 索引名 on 表名

5 常用函数

一、数学函数

函数含义
abs(x)返回x的绝对值
bin(x)返回x的二进制(oct返回八进制,hex返回十六进制)
ceiling(x)返回大于x的最小整数值
exp(x)返回值e(自然对数的底)的x次方
floor(x)返回小于x的最大整数值
greatest(x1,x2,…,xn)返回集合中最大的值
least(x1,x2,…,xn)返回集合中最小的值
ln(x)返回x的自然对数
log(x,y)返回x的以y为底的对数
mod(x,y)返回x/y的模(余数)
pi()返回pi的值(圆周率)
rand()返回0到1内的随机值,可以通过提供一个参数(种子)使rand() 随机数生成器生成一个指定的值。
round(x,y)返回参数x的四舍五入的有y位小数的值
sign(x)返回代表数字x的符号的值
sqrt(x)返回一个数的平方根
truncate(x,y)返回数字x截短为y位小数的结果

二、聚合函数(常用于group by从句的select查询中)

函数含义
avg(col)返回指定列的平均值
count(col)返回指定列中非null值的个数
min(col)返回指定列的最小值
max(col)返回指定列的最大值
sum(col)返回指定列的所有值之和
group_concat(col)返回由属于一组的列值连接组合而成的结果

三、字符串函数

函数含义
ascii(char)返回字符的ascii码值
bit_length(str)返回字符串的比特长度
concat(s1,s2…,sn)将s1,s2…,sn连接成字符串
concat_ws(sep,s1,s2…,sn)将s1,s2…,sn连接成字符串,并用sep字符间隔
insert(str,x,y,instr)将字符串str从第x位置开始,y个字符长的子串替换为字符串instr,返回结果
find_in_set(str,list)分析逗号分隔的list列表,如果发现str,返回str在list中的位置
lcase(str)或lower(str)返回将字符串str中所有字符改变为小写后的结果
left(str,x)返回字符串str中最左边的x个字符
length(s)返回字符串str中的字符数
ltrim(str)从字符串str中切掉开头的空格
position(substr,str)返回子串substr在字符串str中第一次出现的位置
quote(str)用反斜杠转义str中的单引号
repeat(str,srchstr,rplcstr)返回字符串str重复x次的结果
reverse(str)返回颠倒字符串str的结果
right(str,x)返回字符串str中最右边的x个字符
rtrim(str)返回字符串str尾部的空格
strcmp(s1,s2)比较字符串s1和s2
trim(str)去除字符串首部和尾部的所有空格
ucase(str)或upper(str)返回将字符串str中所有字符转变为大写后的结果

四、日期和时间函数

函数含义
curdate()或current_date()返回当前的日期
curtime()或current_time()返回当前的时间
date_add(date,interval int keyword)返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:selectdate_add(current_date,interval 6 month);
date_format(date,fmt)依照指定的fmt格式格式化日期date值
date_sub(date,interval int keyword)返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:selectdate_sub(current_date,interval 6 month);
dayofweek(date)返回date所代表的一星期中的第几天(1~7)
dayofmonth(date)返回date是一个月的第几天(1~31)
dayofyear(date)返回date是一年的第几天(1~366)
dayname(date)返回date的星期名,如:select dayname(current_date);
from_unixtime(ts,fmt)根据指定的fmt格式,格式化unix时间戳ts
hour(time)返回time的小时值(0~23)
minute(time)返回time的分钟值(0~59)
month(date)返回date的月份值(1~12)
monthname(date)返回date的月份名,如:select monthname(current_date);
now()返回当前的日期和时间
quarter(date)返回date在一年中的季度(1~4),如select quarter(current_date);
week(date)返回日期date为一年中第几周(0~53)
year(date)返回日期date的年份(1000~9999)
# 示例:
# 获取当前系统时间:select from_unixtime(unix_timestamp());
select extract(year_month from current_date);
select extract(day_second from current_date);
select extract(hour_minute from current_date);
# 返回两个日期值之间的差值(月数):
select period_diff(200302,199802);
# 在mysql中计算年龄:
select date_format(from_days(to_days(now())-to_days(birthday)),'%y')+0 as age from employee;
# 如果brithday是未来的年月日的话,计算结果为0。
# 下面的sql语句计算员工的绝对年龄,即当birthday是未来的日期时,将得到负值。
select date_format(now(), '%y') - date_format(birthday, '%y') -(date_format(now(), '00-%m-%d') <date_format(birthday, '00-%m-%d')) as age from employee

五、加密函数

函数含义
aes_encrypt(str,key)返回用密钥key对字符串str利用高级加密标准算法加密后的结果,调用aes_encrypt的结果是一个二进制字符串,以blob类型存储
aes_decrypt(str,key)返回用密钥key对字符串str利用高级加密标准算法解密后的结果
decode(str,key)使用key作为密钥解密加密字符串str
encrypt(str,salt)使用unixcrypt()函数,用关键词salt(一个可以惟一确定口令的字符串,就像钥匙一样)加密字符串str
encode(str,key)使用key作为密钥加密字符串str,调用encode()的结果是一个二进制字符串,它以blob类型存储
md5()计算字符串str的md5校验和
password(str)返回字符串str的加密版本,这个加密过程是不可逆转的,和unix密码加密过程使用不同的算法。
sha()计算字符串str的安全散列算法(sha)校验和
# 示例:
select encrypt('root','salt');
select encode('xufeng','key');
select decode(encode('xufeng','key'),'key');#加解密放在一起
select aes_encrypt('root','key');
select aes_decrypt(aes_encrypt('root','key'),'key');
select md5('123456');
select sha('123456');

六、控制流函数

# mysql有4个函数是用来进行条件操作的,这些函数可以实现sql的条件逻辑,允许开发者将一些应用程序业务逻辑转换到数据库后台。
1case when[test1] then [result1]...else [default] end
    如果testn是真,则返回resultn,否则返回default
2case [test] when[val1] then [result]...else [default]end  
    如果test和valn相等,则返回resultn,否则返回default
3if(test,t,f)   
    如果test是真,返回t;否则返回f
4、ifnull(arg1,arg2) 
    如果arg1不是空,返回arg1,否则返回arg2
5、nullif(arg1,arg2) 
    如果arg1=arg2返回null;否则返回arg1
# if()函数在只有两种可能结果时才适合使用。然而,在现实世界中,我们可能发现在条件测试中会需要多个分支。在这种情况下,mysql提供了case函数,它和php及perl语言的switch-case条件例程一样。
# 示例:
mysql>select case 'green'
     when 'red' then 'stop'
     when 'green' then 'go' end;
# 一个登陆验证
select if(encrypt('sue','ts')=upass,'allow','deny') as loginresultfrom users where uname = 'sue';

七、格式化函数

函数含义
date_format(date,fmt)依照字符串fmt格式化日期date值
format(x,y)把x格式化为以逗号隔开的数字序列,y是结果的小数位数
inet_aton(ip)返回ip地址的数字表示
inet_ntoa(num)返回数字所代表的ip地址
time_format(time,fmt)依照字符串fmt格式化时间time值
# 示例:
select format(34234.34323432,3);
select date_format(now(),'%w,%d %m %y %r');
select date_format(now(),'%y-%m-%d');
select date_format(19990330,'%y-%m-%d');
select date_format(now(),'%h:%i %p');
select inet_aton('10.122.89.47');
select inet_ntoa(175790383);

八、类型转化函数

# 为了进行数据类型转化,mysql提供了cast()函数,它可以把一个值转化为指定的数据类型。类型有:binary,char,date,time,datetime,signed,unsigned
# 示例:
select cast(now() as signed integer),curdate()+0;
select 'f'=binary 'f','f'=cast('f' as binary);

九、系统信息函数

函数含义
database()返回当前数据库名
benchmark(count,expr)将表达式expr重复运行count次
connection_id()返回当前客户的连接id
found_rows()返回最后一个select查询进行检索的总行数
user()或system_user()返回当前登陆用户名
version()返回mysql服务器的版本
# 示例:
select database(),version(),user();
# 该例中,mysql计算log(rand()*pi())表达式9999999次。
selectbenchmark(9999999,log(rand()*pi()));

6 事务(Mysql中InnoDB支持)

  • 原子性(Atomicity):原子意为最小的粒子,或者说不能再分的事物。数据库事务的不可再分的原则即为原子性。
  • 一致性(Consistency):指数据的规则,在事务前/后应保持一致。
  • 隔离性(Isolation):简单点说,某个事务的操作对其他事务不可见的。
  • 持久性(Durability):当事务完成后,其影响应该保留下来,不能撤消。
1、开启事务(start transaction)
2、执行sql操作(普通sql操作)
3、提交/回滚(commit/rollback)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值