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 ...... and.....
查询年龄在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外键约束
建表时生成外键 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';

以上是学习数据库以来简单的总结

猜你喜欢

转载自www.cnblogs.com/huanghaobing/p/10027504.html