数据库mysql基础CRUD

字符集和存储引擎

  • 修改字符集
    为了能够正常显示中文,必须把数据库的字符集设置为utf8.
 show variables like 'character%';#查看字符集
修改mysql的配置文件
cd /etc/mysql/mysql.conf.d
sudo cp mysql.cnf mysql.cnf.bak
sudo vim mysql.cnf
在[mysqld]下增加语句:
character_set_server = utf8
保存并重启服务
sudo systemctl restart mysql.service #重启服务
  • 数据库引擎
    常用的数据库引擎:myisam、innodb、archive、ndb、memory
    myisam和innodb的区别
    myisam查询速度快,不支持事务、不支持外键、支持表锁
    innodb增删改效率快,支持事务、支持外键,支持表锁

SQL

基本可分为:
数据定义语言DDL (create、drop)
数据操作语言DML(insert、delete、update)
数据查询语言DQL(select、where、group by、order by 、limit)
数据控制语言DCL(grant、revoke)
事务处理语言TPL(commit、rollback)

mysql基本命令

连接mysql数据库的命令

mysql -h服务器地址 -u用户名 -p

#1.查看库
show databases;
#2. 创建库
create database 数据库名 default charset=utf8;# 数据库名不要纯数字,不要汉字
#3. 删除库
drop database 数据库名;
#4. 选中库
use 数据库名;
#5. 查看表
show tables;
#6.查看数据库创建语句
show create database 数据库名
#7.查看选中的数据库
select database()
#8 修改数据库字符集
alter database student default charset=utf8;

mysql中的C

create table [if not exists] 表名(
 列名1 类型 [限制],
 列名2 类型 [限制],
 ...
 列名n 类型 [限制] #最后一列没有逗号
) [engine=myisam | innodb][ default charset=utf8];

primary key 主键 不允许有重复值,不允许为空
auto_increment 自增长,只对int型主键起作用
primary key 不允许空值,唯一
not null 非空
unique 唯一
default 缺省,默认值

写法一:insert into 表名(字段1,字段2...) values(1,2...);
省略了字段列表,则按照建表时的字段顺序进行插入,每一列都要给值
写法二:insert into 表名 values(1,2...);
写法三:插入多个记录
 insert into 表名(字段1,字段2...)
 values(1,2...),
 (1,2...),
 (1,2...)....

mysql中的R

查看建表语句

show create table 表名;

查看表结构

desc 表名;

数据查询

  • 基础查询
select username,password from user;
select usernname as ⽤户名, password as 密码 from user; #可以给字段起
别名
select * from user; #查询所有字段,慎用,一般不建议使用,会导致无法优化
sql语句
select 2018,username,password from user; #可以有常量,表达式
select sname,2018-year(sbirthday) from student; #year是mysql的内置函数
select distinct username from user; #去除重复记录 distinct 针对查询结果去除重复记录,不针对字段
  • 条件查询
    关系运算符:> 、 >=、 <、 <=、 =、!=、<>、 between and
select username,password from user where uid <10
select username,password from user where uid != 10
select username,password from user where uid between 10 and 20

逻辑运算符:and 、or、not

select username,password from user where uid < 100 and uid > 20;
select username,password from user where uid > 100 or uid < 20;

集合运算符:in、not in

select username,password form user where uid in (2,3,4)
select username,password form user where uid not in (2,3,4)

判空运算:is null、is not null

select username,password from user where username is null

通配符 _代表一个字符,%代表任意长度字符串

select * from user where username like '王_';
select * from user where username like '王%';
  • 排序
select * from user order by age asc;
select * from user order by age desc;
多字段排序
 select name,age from php_user_history order by age desc,name;
#如果在第一列上有相同的值,在具有相同的age的记录上再按name升序排列
  • 限制结果集(limit)
    limit n #取前n条记录
    limit offset,n #从第offset条开始取,取n条
select * from php_user_history limit 3;
select * from php_user_history limit 4,2;
注意结果集中记录从0开始数数,offset相对于0开始
实现分页必须的技术点
limit (page-1)*num,num
  • 集合函数
    • count统计结果集中记录数
    • max 最大值
    • min 最小值
    • avg 平均值,只针对数值类型统计
    • sum 求和,只针对数值类型统计
    • 注意,集合函数不能直接使用在where后面的条件里,但可以在子查询中
select count(*) num from user;
select count(distinct age) num from user; //去除重复记录
select * from student where sno = max(sno);//错误
  • 分组
    • 出现了groub by的查询语句,select后面的字段只能是集合函数和group by后
      面有的字段,不要跟其它字段
    • 对分组进行过滤,可以使用having
select uid, count(*) num from php_forum group by uid;
select uid,title, count(*) num from forum group by uid having
count(*) >=2;
havingwhere的区别:
 where针对原始表进行过滤
 having 是针对分组进行过滤

select 字段 from 表名 [where 条件][group by ] [having][order by ] [limit]

mysql中的U

  • 修改字段类型
 alter table 表名 modify 字段名 类型 [限制] #增加字段
 alter table 表名 add [column] 字段名 类型 [限制];
  • 删除字段
 alter table 表名 drop [column] 字段名;
  • 修改字段名和类型
alter table 表名 change [column] 旧字段名 新字段名 类型 [限制];
  • 修改表名
alter table 表名 rename 新表名
 alter table 表名 [engine=myisam] [default charset=utf8];
  • 可以通过first、after指定插入位置
 alter table student add sno varchar(3) not null after sid; //在sid列后插入
 alter table student add sid int primary key auto_increment first;//在第一列插入

 update 表名 set 字段1=1,字段2=2... where 条件 #不加where修改的是所有的记录

mysql中的D

删除表中的数据,自增主键的值不会重新开始

delete from 表名 where 条件;#如果不加条件,会删除表中所有数据,慎重使用

清空表,自增主键的值重新开始编号
truncate

truncate table 表名,清空表中所有记录,等价于delete from 表名;

delete和truncate差别,truncate后,表中自增主键值从1开始

发布了44 篇原创文章 · 获赞 0 · 访问量 1226

猜你喜欢

转载自blog.csdn.net/weixin520520/article/details/104516850