本文共 6234 字,大约阅读时间需要 20 分钟。
//第一种方式//insert into 表名(字段名,...) values (值,...)insert into employees(employee_id , first_name , last_name)values ('101100S', 'LIHU' , 'A'),('101100S', 'LIHU' , 'A'),...//第二种方式//insert into 表名 set 字段 = 值 , 字段 = 值 ,...insert into employees set employee_id = '155861A' , first_name = 'asdcasc',...
方式一:①要求值的类型与字段类型须一致或者兼容,字段个数不一定与原始表字段个数和顺序一致,但要保证字段与值一一对应 ②可以通过省略字段或者值使用null的方式插入null值 ③字段名省略则表示默认所有列
两种方式的区别在于:方式一支持一次插入多行,且方式一支持子查询:
//insert into 表名 //查询语句;insert into employees(select * from employees where employee_id = 100);
//update 表名 set 字段 = 值 , 字段 = 值...【where 筛选条件】update employees set employee_id = 1000 where employee_id = 100;
(2)修改多表记录
/* update 表1 别名 left|right|inner join 表2 别名 on 连接条件 set 字段=值,字段=值 【where 筛选条件】;*/update employee e join departments don e.department_id = d.department_idset e.first_name = 'wuaksd' , d.department_title = 'asd'where employee_id = 100;
//delete语句//单表删除(以行为单位删除):delete from 表名 【where 筛选条件】delete from employees where salary < 10000;//多表删除,与update类似,但有不同之处/*delete 别名1 , 别名2from 表1 别名1 inner|left|right join 表2 别名2 on 连接条件【where 筛选条件】*/delete e, dfrom employees ejoin departments don e.department_id = d.department_idwhere e.salary < 10000;
(2)方式二:使用truncate
//truncate table 表名truncate table employees
两种方式的区别:
①truncate删除后,再进行数据插入时,表示列从1开始,而delete从断点开始 ②delete可以添加筛选条件,而truncate不可以,只能将表中数据全部删除 ③truncate效率高且没有返回值,delete可以返回受到影响的行数 ④truncate不能回滚,delete可以回滚//创建库create database 【if not exists】库名 【character set 字符集名】;//修改库的字符集(对库的修改比较少)alter database 库名 character set 字符集名alter database myemployees character set gbk//删除库drop database 【if exists】 库名
//创建表/*create table 【if not exists】 表名(字段名 字段类型 【约束】字段名 字段类型 【约束】...)*/create table if not exists employees(employee_id int ,first_name varchar(20) ,last_name char(10))//修改表//添加字段即列alter table 表名 add column 列名 类型 【first|after 字段名】;//修改字段的类型或约束alter table 表名 modify column 列名 新类型 【新约束】//修改列名alter table 表名 change column 旧列名 新列名 类型//删除列alter table 表名 drop column 列名//修改表名alter table 表名 rename to 新表名//删除表drop table if exists 表名//复制表//仅仅复制表的结构而非表中数据create table 新表名 like 旧表:create table newemployees like employees;//复制表的数据+结构create table 新表名 select * from 旧表where 筛选条件
create table if not exists biao1(id int(10) zerofill unsigned);
②浮点型
定点数:decimal(M,D) 浮点数:float(M,D),double(M,D) 特点: M代表整数部位+小数部位的个数,D代表小数部位 如果超出范围,则报out or range异常,并且插入临界值 M和D都可以省略,但对于定点数,M默认为10,D默认为0 如果精度要求较高,则优先考虑使用定点数(2)字符型
char、varchar、binary、varbinary、enum、set、text、blob char:固定长度的字符,写法为char(M),最大长度不能超过M,其中M可以省略,默认为1 varchar:可变长度的字符,写法为varchar(M),最大长度不能超过M,其中M不可以省略(3)日期型
year年 date日期 time时间 datetime 日期+时间 8 timestamp 日期+时间 4 比较容易受时区、语法模式、版本的影响,更能反映当前时区的真实时间#方式一:级联删除ALTER TABLE stuinfo ADD CONSTRAINT fk_stu_major FOREIGN KEY(majorid) REFERENCES major(id) ON DELETE CASCADE;#方式二:级联置空ALTER TABLE stuinfo ADD CONSTRAINT fk_stu_major FOREIGN KEY(majorid) REFERENCES major(id) ON DELETE SET NULL;
(1)创建表时添加约束
除了外键之外,所有的约束都可以添加为列级约束(不可以起约束名);除了非空与默认约束,所有的约束都可使添加为标记约束(可以起约束名,但对主键无效)。因此通常是将外键添加为标记约束,其他添加为列级约束。create table 表名(//列级约束 字段名 字段类型 not null, #非空 字段名 字段类型 primary key,#主键 字段名 字段类型 unique, #唯一 字段名 字段类型 default 值, #默认//表级约束 constraint 约束名 primary key(字段名), constraint 约束名 foreign key(字段名) references 主表(被引用列))
(2)修改表时添加或者删除约束
//添加或删除非空约束alter table 表名 modify column 字段名 字段类型 not null;//添加约束alter table 表名 modify column 字段名 字段类型;//删除约束//添加或删除默认约束alter table 表名 modify column 字段名 字段类型 default 值;alter table 表名 modify column 字段名 字段类型;//添加或删除主键约束alter table 表名 add column 【constraint 约束名】primary key(字段名);alter table 表名 drop primary key;//添加或删除唯一键约束alter table 表名 add column 【constraint 约束名】unique(字段名);alter table 表名 drop index 唯一索引名;//添加或删除外键约束alter table 表名 add column 【constraint 约束名】foreign key(字段名) reference 主表(被引用列);alter table 表名 drop foreign key 约束名;
(3)自增长列
特点: ①不用手动插入值,可以自动提供序列值,默认从1开始,步长为1 auto_increment_increment 如果要更改起始值:手动插入值 如果要更改步长:更改系统变量 set auto_increment_increment=值; ②一个表至多有一个自增长列 ③自增长列只能支持数值型 ④自增长列必须为一个key//创建表时设置自增长列:create table 表名(列名 类型 约束 auto_increment,列名 类型 约束,...)//修改表时设置自增长列alter table 表 modify column 列名 类型 约束 auto_increment//删除自增长列alter table 表名 modify column 列名 类型 约束
//开启事务set autocommit = 0;//(只针对当前会话或连接)start transaction ;//(该语句可选)//编写事务中的SQL语句(select, insert , update , delete)update account set banlance where id = 100;savepoint point1;//设置回滚点update account set balance where id = 1005;//结束事务commit;//提交事务rollback point1;//回滚事务到回滚点处
set autocommit = 0;start transaction ;delete from employees;rollback;//使用delete删除表中数据,执行回滚后表中数据依然存在set autocommit = 0;start transaction ;truncate table employees;rollback;//使用truncate删除表中数据,无法执行回滚
并发事务
在同时执行多个事务访问相同数据库时会产生并发问题,并发问题分为以下几种 ①脏读:一个事务读取了其他事务还没有提交的数据,读到的是其他事务更新的数据 ②不可重复读:同一事务多次读取,结果却不同 ③幻读:一个事务读取了其他事务还没有提交的数据,只是读到了其他事务插入的数据隔离级别
那么如何解决并发问题呢?可以通过设置隔离级别来解决并发问题隔离级别 脏读 不可重复读 幻读read uncommitted × × ×read committed √ × ×repeatable read √ √ √ //是MySQL的默认隔离级别serializable √ √ √//查看当前数据库的隔离级别select @@tx_isolation//设置隔离级别set transaction isolation level 隔离级别//设置数据库系统全局隔离级别set global transaction isolation level 隔离级别
转载地址:http://bweg.baihongyu.com/