linux中进入mysql命令:
mysql -u+(用户名) -p+(密码 )
mysql语句命令后面一定要加“;”
查询库名:
show databases;
修改库:
use 库名;
创建库:
create database 【if not exists】【库名】;
查询创建库:
show create database 【库名】;
删除库:
drop databse 【库名】
查询创建表:
show create table 【表名】;
查询当前所使用的库:
select database();
创建表:
create table 【if not exists】表名(属性)
查询表:
desc 【表名】;
删除表 :
drop table 【表名】
例如:
create table student(int id(长度)(额外属性),name varchar(长度) );
数据库中 int类型默认长度是11
varchar不给长度会报错
不为空:
not null
自增:
auto_increment
主键:
primary key(属性名)
属性之间以“ ,”分割
字符集:
charset 【类型】
存储引擎:
engin
Alter用法
ALTER TABLE (表名) [选项] column datatype [DEFAULT expr];
1.添加:add
例如:
alter table Student add work varchar(255);
添加索引: alter table 表名 add index 索引名称(字段);
2.删除:drop
alter table Student drop work varchar(255);
删除索引: alter table 表名 drop index 索引名称;
3.修改表属性类型:
modify:
alter table [表名] modify [属性名] [类型];
4.修改属性名
change:
改表中属性名:alter table Student change [旧表名] [新表名] [类型] ;
例如:alter table Student change age gender varchar(255
5.改表的字符集:
alter table [旧表名] charcater set [类型];
6.改库的字符集:
alter database [旧库名] charcater set [类型];
7.修改表名
alter table 【旧表名】 rename to 【新表名】;50000000
mysql语句
添加数据
insert into 表名(属性)values(数据);
例如:
insert into Student(name,gender,job)values('张三','18','学生');
```
可以添加多条: insert into Student(name,gender,job)values('张三','18','学生'),('李四','23','工人');
查询表中数据
select 【字段】from 表名
* 表示查询全部字段
where: 表示筛选条件
having:另外having是group by 专用筛选语句,
where是对表中的数据做筛选,而having是对select 后面的数据筛选,是对where后面做补充 where优先级更高
查询编号为1的数据 例如:select * from Student where id='1';
between 在---之间:
例如 :SELECT * from Student WHERE ID BETWEEN 2 AND 3;
in(set) 显示in列表中的值:
例如 select *from Student where age in(21,22);
like 模糊查询使用 % 和 _
例如:select *from Student where name='张%';
select *from Student where name='张_';匹配叫张张的学生;
IS NULL 判断是否为空
and 多个条件同时成立
or 对各条件 任一成立
not 不成立 例如: select * from where not (age>18) 查询年龄小于十八的
limit[起始位置],[查询个数]
select * from student limit 5,5;表示从第五个开始查询,向后查询5个
order by 命令
desc 降序
asc 升序
select * from sudent order by age desc limit 0,3;把年龄按降序排序,查询从上到下三个,也就是查询年龄最大的三个
注意 order by age ,id desc; 这样age默认升序,id降序
order by age desc, id desc; 全部降序,但排序还是以第一个命令为准,当第一个有同样
的数据是,在排序后面的
group by :分组
后面跟having 是专用 如果用where就会报错
例如: select count(*) ,sex form student group by sex 按年龄分组
group by 一般与聚合函数共同使用
distinct 去重
注意:字段只能写在distinct后面当做去重来显示,不能放在distinct前面单独显示了
例如: select distinct name ,sex from student;
聚合函数
max :最大值
min: 最小值
avg: 平均值
count:数量
sum: 总数 注意(varchar数据只有数字能计算数量,其他都为0)
拼接 concat(str1,srt2,...)
例如:select id,name ,concat(address01,'-',address0) from student;
concat_ws(separator,str1,str2,...)
例如:select id,name,concat_ws(':',address0,address02,name) from student;
计算 一般可以直接计算 select price*count from student;
获取时间
select current_timestamp;
select current_date;
select current_time;
select current_date();
时间转var
date_format(date,format)
select date_format(current_date(),'%y-%m-%d');
var转date
'2021-11-30'
select str_to_date('2021-11-30','%y-%m-%d')
注意格式要匹配:例如 2021-11-30 用%y:%m:%d就不行
时间加减
datediff(expr1,expr2);expr1-expr2没办法相加,只能两个时间相减
date_add(date,interval expr unit)
expr是参数
unit是加减单位:day 天
month 月
year 年
保留小数
round:四舍五入
select round(expr1,expr2);
expr1:代表参数
expr2:代表保留几位小数
ceil(x):向上取整
floor(x):向下取整
判断语句 when a then b else c end from student;
例如 : select when age=20 then 21 else 22 end
如果else then 不给值 则返回空
mysql 三大范式
1.字段不可再分割 原子性
2.字段依赖于主键 唯一性
3.避免数据量过大
left join on 以左边为准
right join on 以右边为准
inner jion on 中间为准
mysql复制
复制一个表:
例如:create table show1 as select * from student; 就相当于将student复制成show1,
注意:复制完成后show1中没有student中字段的主键
插入一个表
例如:insert into show1 select * from student2;就是将student2中的数据给show1
注意:插入数据 show1和student2中字段类型必须一致。不然会报错,插入式按照顺序插入
创建视图
视图不占内存
create view show2 as select * from student2
注意:如果视图来自一张表则可以增删改查,如果来自多个表,则只能查询
事务
bigin:表示开始
rollback:表示回滚
commit:表示提交
mysql进阶–shell操作
#!/bin/sh
mysql=“mysql -h192.168.186.30 -uroot -123456”
sql=“select *from 库名.表名”
result=“ ( ( (mysql -e “$sql”)”
echo -e “$result”