当前位置:Gxlcms > 数据库问题 > mysql基础语句

mysql基础语句

时间:2021-07-01 10:21:17 帮助过:9人阅读


mysql -u root -p

--停止mysql
net stop mysql

--开启mysql
net start mysql

--ddl 数据定义语言 create alter drop
--dml数据操作语言 update delete insert
--dql数据查询语言 select
--dcl数据控制语言 savepoint commit rollback grant revoke

--创建数据库
create database shop character set utf8 collate utf8_general_ci;

--删除数据库
drop database shop;

--查看当前数据库链接进程情况
show processlist;

--修改数据库 *只能修改字符集或者校验规则*
alter database shop character set gbk collate gbk_general_ci;

--备份数据库
--在默认指令下
mysqldump -u root -p shop>c:/mysql.sql

--恢复数据库 *单库*
①create database shop1;
②use shop1;
--在mysql指令下
③source c:/mysql.sql;
④show tables; -- *查看数据是否完整*

--备份指定数据库的某张表
mysqldump -u root -p shop(数据库名) goods(表名)>c:/mysql_goods.bak

--恢复表 *跟单库恢复一样*
source c:/mysql_goods.bak;

--一次备份多个库
mysqldump -u root -p -B shop(数据库一) shop1(数据库二) > c:/mysql.bak

--恢复多个库备份
source c:/mysql.bak;

--创建表
create table goods(
id int not null unsigned auto_increment default 0 primary key
name varchar(32) not null,
class_id int(6) zerofill
)character set utf8 engine=myisam;

--enum(枚举) set(集合)
create table people(
id int not null unsigned auto_increment default 0 ,
name varchar(32) not null,
sex enum(‘男‘,‘女‘) not null,
hobby set(‘打球‘,‘玩游戏‘,‘跑步‘,‘开车‘) not null
)character set utf8 engine = innodb;

--添加数据
insert into people values(1,"张三","男","玩游戏,开车,跑步");

--set enum数据查找
select * from shop where sex="男";
select * from shop where find_in_set(‘泡妞‘,hobby);

--存放 图片 视频 音频等文件
create table shop2(
img varchar(64) not null "/imgs/01.jpg"
)charset=utf8 engine=innodb;

--插入
insert into shop2 values(‘imgs/abc.jpg‘);

--创建一张学生信息表
create table stu_info(
id int unsigned not null auto_increment default 0,
name varchar(32) not null,
sex enum(‘男‘,‘女‘,‘不知道‘) not null,
brithday date not null,
entry_day date not null,
job varchar(32) not null,
salary decimal(10,2) not null,
resume text not null
)charset=utf8 engine=innodb;

 

--********************************** mysql 用户管理 ***************************************
create user "用户名"@"主机名" identified by "密码" 创建mysql用户
drop user "用户名"@"主机名"; 删除用户
set password = password("密码"); 给自己修改密码
set password for "用户名"@"主机名" = password("密码"); 给别人修改密码

grant 权限列表 on 库.表名 to "用户名"@"主机名" ; 给权限
revoke 权限列表 on 库.表名 from "用户名"@"主机名" 回收权限
show grant for "用户名"@"主机名"; 查看有什么权限

 

--************************************ 视图***************************************
create view 视图名 as select 语句 创建视图
drop view 视图名 删除视图
alter view 视图名 as select 语句 修改视图
--********************************** 表 ***************************************
--修改表
--修改表之增加列 *后可跟 after 排在那个列之后*
alter table stu_info(表名) add hobby(新加列名) set(‘游泳‘,‘戏水‘,‘跳舞‘) not null after sex ;
--修改表之删除列
alter table stu_info(表名) drop hobby(列名);
--修改表之修改列
alter table stu_info(表名) change hobby(旧列名) hob(新列名) set(‘赌博‘,‘抽烟‘) not null;
--修改表之增加主键
alter table stu_info(表名) add primary key (增加主键的列);
--修改表之删除主键
alter table stu_info drop primary key;
--修改表之删除外键
alter table stu_info drop foreign key(外键名);
--修改表之增加索引
alter table stu_info add (index/unique) (索引名/列名);
--修改表之删除索引
alter table stu_info drop index (索引名);
--修改表之清空表数据
truncate table stu_info;
--修改表之更改表名
rename table stu_info to stu;
--修改表之更改字符集
alter table stu_info charset = gbk;

--查看表结构
desc stu_info;
show create table stu_info;


--********************************** 数据 ***************************************
--添加数据
insert into stu_info values(1,"张三","男","吃饭,打架",‘2016-03-04‘,‘2014-03-04‘,‘能吃能打能睡觉‘);
--修改数据
update stu_info(表名) set name="李四"(修改的字段/值) where(通过条件找到更改的数据) id =1 ;
--删除数据
delete from stu_info(表名) where(通过条件找到删除的数据列) id = 1;
--查询数据
select distinct(去重复) * from stu_info;

--查询数据可以带运算
select (salary = salary*3) from goods;
--查询数据可以起别名
select (salary = salary*3) as sal from goods;
--查询数据可以有函数
select avg(salary) as avg_sal from goods;


--********************************** where子句 ***************************************
--条件过滤
select * from stu_info where id =1;
--可以使用and or not
select * from goods where sal > 1500 and id <4;
select * from goods where sal>500 or sal <2000;
select * from goods where not (sal > 100);
--可以使用 between ... and ... 显示某一区间的值
select sal from goods where sal between 500 and 2000;

--********************************** order by子句 ***************************************
--排序 *desc 降序 asc 升序*
select * from stu order by yuwen;

--********************************** group by子句 ***************************************
--分组统计 *对于分组统计后的数据用having 过滤 *
select * from stu group by yuwen;

select deptno,avg(sal) as avg_sal from emp group by deptno having avg_sal<2000;

--********************************** like子句 ***************************************
--%表示任意0到多个字符
select * from stu where name like "王%";
-- _ 表示任意单个字符
select * from stu where name like "_三%";

--********************************** mysql函数 ***************************************

--********************************** 聚合函数 ***************************************
--count() *记录满足条件的记录数 count(*)统计所有 count(列名) 不会统计null值*
select count(*) from stu;
select count(*) from stu where yuwen>50;

--sum() *记录总和 sum(列名) ()内不能用* null+任何值都为null *
select sum(yuwen) from stu;

--avg() *记录平均值 ()内不能为* *
select avg(yuwen) from stu;

--max(最大值)/min(最小值)
select max(yuwen) from stu;
select min(yuwen) from stu;

--********************************** 日期函数 ***************************************

--current_date() *查询当前日期 显示年月日*
select current_date();
--current_time() *查询当前时间 显示时分秒*
select current_time();
--current_timestamp() *查询当前时间戳 显示当前年月日时分秒*
select current_timestamp();
--now() *获取当前时间 显示年月日时分秒*

--date *返回datetime的年月日部分*
date(datetime)
--date_add(date,interval val typ) *在date时间上加上val,typ是val的类型 可以是 年 月 日 时 分 秒*
date_add(now(),interval 10 day);
--date_sub(date,interval val typ) *在date时间上减去val, typ是val的类型 可以是 年 月 日 时 分 秒*
date_sub(now(),interval 10 day);
--datediff(date1,date2) *date1和date2的差 返回值类型为 天*
datediff(now(), date_sub(now(),interval 10 day))

--timediff(date1,date2) *date1和date2的时间差 返回值是 多少时多少分多少秒*
timediff(now(),date_sub(now(),interval 10 minute));
--year/month/day/date(datetime) *返回 年/月/日/年-月-日*
year(now()); month(now()); day(now()); date(now());

--unix_timestamp() *返回一个 1970-1-1 0:0:0 到现在时间的一个时间戳 秒*
select unix_timestamp();
--from_unixtime(val,格式) *将val转换成一个你想要的时间格式 val是秒数,格式由程序员自己定 %Y-%m-%d %h-%i-%s *
select from_unixtime(unix_timestamp(),‘%Y-%m-%d‘);

--********************************** 字符串函数 ***************************************
--charset(str) *返回字符集*
select charset(‘abc‘);
--concat(str str str) *链接字符串*
select concat(name,"语文分数是",yuwen) from stu_info;
--ucase(str)/lcase(str) *转换成大写/小写*
ucase("abc"); lcase("ABC");
--length(str) *计算str的长度 单位 字符*
length(‘abc‘)
--replace(str,str1,str2) *把str里面的str1换成str2*
replace("abc",b,d);
--substring(str,val,val2) *在str里 从val开始截取val2个字符 val,val2为整数*
substring("abcdefg",2,4);

--********************************** 数学函数 ***************************************
--abs(int) *求int的绝对值*
abs(-90);
--ceiling(num) *向上求num的整数*
ceiling(34.1456); 35
--floor(num) *向下求num的整数*
floor(34.1456); 34
--format(num,val) *保留小数位数 val是小数后保留几位*
format(78.329,2); 78.33
--mod(num1,num2) *求num1和num2的余数*
mod(10,3) 余 1
--rand() *返回一个随机数 范围是 0到1.0*
select floor(rand()*100);

--********************************** 流程控制函数 ***************************************
--if(ex1,ex2,ex3) *如果ex1为真那么返回ex2否则返回ex3*
if(0==0,1,2);
--ifnull(ex1.ex2) *如果ex1不为null返回ex1 否则返回ex2*
ifnull(null,0);
--case when ex1 then ed1 else ed2 end *ex1为条件,ed为表达式*
select case
when sal<100 then sal*10
else sal
end

--********************************** 其他函数 ***************************************
--user() *查看当前用户*
select user();
--database() *查看当前使用的是那个数据库*
select database();
--md5() *加密方式*
md5("abc");
--password *加密方式*
password("abc");
--mysql_num_fields() *取得结果集中字段的数量*
--mysql_field_name() *取得结果中指定字段的字段名*

--********************************** limit 分页查询 ***************************************
--先设定好 每页显示的条数 $pagesize=10;
--在计算好从第几条开始显示 (你要取出第几页的数据 -1) * 每页显示的条数 $pagenow 代表你要取出第几页的数据
($pagenow-1)*$pagesize,$pagesize
select * from emp limit 8,4;

--********************************** 查询加强--多表多条件查询 ***************************************
--
select avg(sal),deptno
from emp
group by deptno;

 

--事物
--开启事物
start transaction
-- 自动提交关闭
set autocommit=false;
--设置保存点
savepoint 名称
--退回
rollback to 名称

 

 

转载请注明出处  原文博客:https://www.cnblogs.com/we-jack/p/8341120.html

mysql基础语句

标签:datetime   分组   表数据   单个字符   into   arc   min   文件   创建数据库   

人气教程排行