mysql⼀些隐藏的简便⽅法_mysql数据库简单⼀些简单操作和
总结
1. mysql 数据库操作⽅法:
进⼊数据库 mysql -uroot -p
退出 quite exit
默认引擎 innodb
查看版本 select verison();
查看时间 select now();
逻辑运算 select 1+1;
查询数据库 show databases ;
创建数据库 create database 库名 charset = utf8;
删除 drop database 库名;
使⽤某个数据库 use 库名;
查询库中所有表 show tables;
查看表结构 desc 表名;
创建表 create table 表名( 增加字段和数据类型
)
例如: create table classes(
id int unsigned(⽆符号的) primary key(主键) auto_increment (⾃动增加) not null(⾮空),
name varchar(30) not null default(默认)‘ ’
);
修改表名 alter table 表名 rename to 新表名;
删除表 drop table;
查询表内容 select * from 表名;
往表中插⼊数据 insert into 表名 values();
例如 insert into student values(0,'⼩明'),(0,'⼩花') ; 零 是占位。注:有⼏个字段名就插⼊⼏个数据,⽐如前⾯的插⼊了两个
删除数据 delete from 表名 where id = 1;
修改数据 update 表名 set 字段名 = ‘1234’where id = 1; 1234代表新的内容修改ID为1的数据
增加字段 :
alter table 'student'
add column 'num1' int(3) null default null;
修改字段 :
alter table'student'
change column 'num1' 'num2' int(3) null default null;
删除字段:
alter table 'classes'
drop column 'num';
⼀个英⽂字符等于⼀个字节,⼀个中⽂三个字节
2. 连接查询 join
join ⽤于多表中字段之间的联系,语法如下:
from table1 inner或者left或者right join table2 on conditiona 3. 使⽤ as 给字段起别名(给长的字段取别名)
select id as code from student; 可以通过 as 给表起别名
在select后⾯列前使⽤distinct可以消除重复的⾏
4. mysql的联合主键: select 字段1,字段2 from 表名 where 条件逻辑运算符
and or not (is ⽤来判断null)
5. 范围查询
between ......
查询年龄在20岁到30岁之间的学⽣
select name from student where age between 20 and 30
in表⽰在⼀个⾮连续的范围内
select id from student where id in (1,2,3)
select distinct num from student ;
6 . 模糊查询 like
%表⽰任意多个任意字符
_ 表⽰⼀个任意字符
查询姓黄的学⽣
select * from student where name like ‘黄%’
7. 聚合函数
mysql中五种常⽤的聚合函数:
(1)max(列名):求最⼤值。 select max(salary) from salary_tab;
(2)min(列名):求最⼩值。
(2)sum(列名):求和。
(4)avg(列名):求平均值。select sum(salary) from salary_tab;
(5)count(列名):统计记录的条数
①count(*):返回表中满⾜where条件的⾏的数量
select count(*) from salary_tab where salary='1000';
8. 排序
为了⽅便查看数据,可以对数据进⾏排序
语法:order by 字段 asc 或者 desc
查询学⽣信息,按学号降序
select * from student order by id desc
9.分组
分组SELECT的基本格式:
select [聚合函数] 字段名 from 表名
[where 查询条件]
[group by 字段名]
[having 过滤条件]
指定⼀个列进⾏分组
select salary,count(*) from salary_tab
指定多个分组列,‘⼤组中再分⼩组’
select userid,count(salary) from salary_tab
根据表达式分组
select year(payment_date),count(*)
10. having字句
对分组结果进⾏过滤
having⼦语句与where⼦语句区别
where⼦句在分组前对记录进⾏过滤;
having⼦句在分组后对记录进⾏过滤
11. limit 分段取值
语法 limi m,n select * from student limit 0,2 从第⼀个开始取,取两个,如果不写第⼀个参数,就是默认从第⼀个参数取值,取n个12. mysql外键约束mysql数据库的方法
建表时⽣成外键 foreign key('sid') references'student'('id');
建表时添加外键 alter table'course_student' add foreige key('sid') references'student' (''id);
删除外键 alter table'course_student'drop foreign key'course_student_idfk_1';
13. mlsql⾃关联
select * from areas as p inner join areas as c on c.pid = p.aid where p.atile = '河南省';
14.⾃关联
标量⼦查询
select * from arrticle where uid = (select uid from user where status = 1 order by uid desc limit 1);
列⼦查询
select * from student where cls id in (select id from classes where id in (1,2))
⾏⼦查询
⽰例:查询⼀个班⾝⾼最⾼,岁数最⼤的学⽣
select * from student where(age,height) = (select max(age),max(height) from student);
15.视图
建⽴视图 create view 视图名称 as select 语句
建议名称以v_开头,⽤来和普通表区分, 使⽤show tables 可以显⽰视图
使⽤视图 select * from 视图名称
删除视图 drop view 视图名称
修改视图 create or replace view 视图名称 as sql语句
16.事务
开启事务后执⾏修改命令
begin 或者start transaction;
提交事务 commit;
回滚事务 rollback
17.索引
使⽤索引 select * from test_index where name = 'ca-900';
联合索引 select * from test_index where name = 'ca-900' and id = 'ha-900';
以上是学习数据库以来简单的总结

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。