数据库【MySQL常用命令】

Linux下安装及基础命令

yum install mysql 下载MySQL的客户端
yum install mysql-server 下载MySQL的服务端
service mysqld status 查看mysql是否开启
service mysqld start 启动mysql数据库
mysql -uroot -p 进入mysql数据库
quit / exit 退出数据库
show databases; 查看所有数据库
use mysql; 进入mysql数据库
show tables; 查看当前数据库的所有数据表
create database hello; 创建一个叫hello的数据库
create table stu(id int(5),name varchar(5)) 创建了一个名为stu的表
select version(); 查看数据库版本

设置密码:
grant all privileges on . to root@‘%’ identified by ‘1234’ and host = ‘localhost’; 给权限
update user set password=password(‘1234’) where user=‘root’ and host=‘localhost’;
create user karl@‘%’ identified by ‘123456’; 创建用户
create user karl@‘localhost’ identified by ‘123456’;
vim /stc/my.cnf 在mysql后加行写入 skip-grant-tables 跳过密码
flush privileges 设置完后刷新权限
mysqladmin -u root password 123456 修改密码

mysql数据常用类型

1.int 整形 / bigint
2.float 小数/浮点型
3.char 字符
4.varchar 字符
5.date 日期 2020-07-07
6.datetime 时间日期 2020-07-07 11:30:00

常用约束

primary key 主键约束,非空、唯一,如手机号码,身份证号码等
not null 非空约束,字段不能为空
default 默认值约束,默认字段为0,如布尔值初始时默认为0
comment 备注,如student comment ‘学生表’
default charset = utf8 默认字符编码,可支持中文

增删改查

select * from stu order by id desc 倒序排序
select * from stu group by id asc 顺序排序
select * from stu;
select age from stu where name = ‘bill’;
select * from stu where age = ‘18’ and name = ‘bob’
select name from stu where age = ‘18’;
select id from stu where name like ‘%o%’
select * from stu order by id desc
select * from t_product limit 1,3 #数据计算是从0开始数,其查询结果为第二条开始的三行数据
select name,count(*) as total from t_product group by name having total >1 ;

select name from stu where age = (select age from stu where name = ‘tom’) + 12;
select * from user; 查看user表中的所有数据,*表示所有
desc stu 查看stu表结构
drop table stu 删除stu表
insert into stu(id,name) values (‘1’,‘laowang’);在stu表中插入一条数据
update stu set id = ‘1’ where name = ‘laowang’; 修改stu表的id字段,将name为老王的id改成1
delete from stu where id =‘3’; 删除id为3的那行
delete from stu where id is null; 删除id为空的行

聚合函数

select count(id) from stu; #总数
select avg(age) from stu; #平均值
select max(age) from stu; #最大值
select min(age) from stu; #最小值
select sum(age) from stu; #求和
select distinct(age) from stu; #去重

数据库数据备份还原

备份:mysqldump -uroot -p karl > xxx.sql 将kral数据库备份到相对路径下,名为xxx.sql或路径
还原:mysql -uroot -p karl < xxx.sql 将xxx.sql还原到karl数据库中

修改表结构

1.desc stu; 查看学生表
2.alter table stu rename stu1; 修改学生表为学生表1
3.alter table stu change id di; 将学生表中的id改成di,di后面要接表结构
4.alter table stu add sex int primary key after id; 将学生表的id后面加上性别字段,注意加约束;
5.alter table stu drop sex; 将学生表的sex字段删除
6.alter table stu change id int auto_increment ; 修改主键id为自增长
7.drop table stu; 删除学生表
8.create database db character set utf8; 创建数据库并设置编码格式为utf8

连表查询

select * from stu;
select * from test inner join stu on stu.id = test.id;#内连接
select * from test left join stu on stu.id = test.id;#左连接
select * from stu right join test on stu.id = test.id;#右连接
select * from biao inner join test inner join laopo on biao.id = test.id = laopo.id #基本表拼接
select book.,huajian.name from book left join huajian on book.id = huajian.id;先拼接两个表
select * from test1 left join (select book.age,huajian.
from book left join huajian on book.id = huajian.id)a on test1.id = a.id; 拼接三个表

视图与索引

视图view
1.什么是视图 视图是一个虚拟的表,它不在数据库中以存储数据的 形式保存,还是 在使用视图的时候动态生成。
2.视图的特点:视图时由基本表产生的虚表,视图的创建和删除不影响基础表,视图的更新和删除直接影响基础表,当视图内容来源于多个虚拟表不能删除
3.视图的作用 :1.数据库中数据的查询非常复杂,例如多表,子查询, 编写语句较多,并且这种查询常被重复使用,那么我 们就可以创建视图,避免每次写sql语句会错误,也 提高了查询的效率 2.为了安全,在公司中,有些字段为保密字段,那么 可以创建视图,限制对某些字段的操作
视图:
create view shitu as select id,name from emp; 创建一个视图从emp中拾取id和name列
alter view shitu as select earning from empo ; 修改视图内容为 emp中的 earning列
drop view shitu; 删除视图

索引
数据库索引是为了增加查询速度而对表字段附加的一种标识。DB在执行一条Sql语句的时候,默认的方式是根据搜索条件进行全表扫描,遇到匹配条件的就加入搜索结果集合。如果我们对某一字段增加索引,查询时就会先去索引列表中一次定位到特定值的行数,大大减少遍历匹配的行数,所以能明显增加查询的速度。但不一定所有的表都需要进行索引,如不需要进场扫描的表,或者只是储存数据并不调用的表。
索引又分主键索引与唯一键索引
主键索引与唯一索引的关联:
1.相同点:他们对应的字段值不能重复
2.不同点:
一张表只能有一个主键索引,而唯一索引允许多个;
主键索引可以设为外键,而唯一索引不可以
3.查询某个表中有哪些已经创建的索引:
show index from dcs01;
4.创建索引:使用alter table语句创建3种索引。

索引:
alter table table_name add index index_name(column_list); //column_list可以是一个或多个字段名
alter table table_name add unique index_uni_name(column_list);
alter table table_name add primary key(column_list);
alter table t_product add index index_name(name);创建主键索引
show index from t_product; 查看表中索引
alter table t_product drop index index_name; 删除表中索引

存储过程

drop procedure if exists pro_name
create procedure pro_name()
declare i  int default 1;
declare n int default 100;
begin  
	if () then
		select true
	else
		while 循坏语句 do
		end while
	end if
end 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值