温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql基础知识有哪些

发布时间:2021-11-18 11:34:01 来源:亿速云 阅读:147 作者:iii 栏目:MySQL数据库

本篇内容介绍了“mysql基础知识有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

第一章 mysql的安装和配置
1 mysql数据库版本:社区版、企业版
2 mysql数据库类型:standard、max、debug
3 windows下安装mysql:noinstall、图形化方式安装
4 linux下安装mysql:rpm包、二进制包、源码包
5 mysql服务和mysql数据不同,mysql服务是一系列后台进程,而mysql数据库则是一系列的数据目录和文件;mysql数据库必须在mysql服务启动之后才可以进行访问。这点和oracle类似。
6 linux平台下启停mysql服务:
    命令行:
        启动:# cd /usr/bin
                 # ./mysqld_safe &
        关闭:# mysqladmin -uroot shutdowm
    服务的方式:
        启动:# service mysql start
        关闭:# service mysql stop
第二章 sql基础
1 SQL :structure query language
2 sql分类:DDL、DML、DCL
3 连接mysql:$ mysql -uroot -p**** -hlocalhost -P3306
4 your mysql connection id is 7344941:这个数字记录了mysql服务到目前为止的连接次数,每个新连接都会自动加1
5 创建数据库:mysql> create database test1;
6 查看当前有多少数据库: mysql> show databases;
7 默认数据库的功能:
    information_schema:主要存储了系统中的一些数据库对象信息,比如用户表信息、列信息、权限信息、字符集信息、分区信息等。
    cluster:存储了系统的集群信息
    mysql:存储了系统的用户权限信息
    test:系统自动创建的测试数据库,任何用户都可以使用。
8 选择数据库并查看该库中的表:
    mysql> use test1
    mysql> show tables;
9 查看当前的数据库信息以及用户信息:mysql> status
10 删除数据库:mysql> drop database test1;
11 创建表:
    mysql> create table emp(ename varchar(10),hiredata date,sal decimal(10,2),deptno int(2));
    mysql> desc emp;
    mysql> show create table emp \g;
    PS:\g含义是使得记录能够按照字段竖向排列,以便更好地显示内容较长的记录。
12 删除表:mysql> drop table emp;
13 修改表:
    修改表字段属性:alter table emp modify ename varchar(20);
    增加表字段:alter table emp add column age int(3);
    删除表字段:alter table emp drop column age;
    表字段改名:alter table emp change age age1 int(4);
    修改表字段排列顺序:alter table emp add birth data after ename;alter table emp modify age int(3) first;
    更改表明:alter table emp rename emp1;
14 插入记录:
    insert into emp(ename,hiredata,sal,deptno) values('zzx1','2000-01-01','2000',1);
    insert into emp values('lisa','2003-02-01','3000',2);
    insert into dept values(5,'dept5'),(6,'dept6');
15 更新记录:
    update emp set sal=4000 where ename='lisa';
    PS:注意要带where子句,不然会把表中所有行该字段都更新为lisa,很多人不小心都会犯这个错误。
16 删除记录:
    delete from emp where ename='dony';
    delete a,b from emp a,dept b where a.deptno=b.deptno and a.deptno=3;
17 查询记录:
    select * from emp;
    slelect ename,hiredate,sal,deptno from emp;
    select distinct deptno from emp;
    select * from emp where deptno=1 and sal<3000;
    select * from emp order by sal;  desc表示按照字段进行降序排列,asc表示升序排列。默认是按照升序排列。
    select * from emp order by deptno,sal desc;
    select * from emp order by sal limit 3;
    select * from emp order by sal limit 1,3;
    sum(求和),count(记录数),max(最大值),min(最小值)
    with rollup 是可选语法,表明是否对分类聚合后的结果进行再汇总。
    having和where的区别在于,having是对聚合后的结果进行条件过滤,而where是在聚合前就对记录进行过滤,如果逻辑允许,我们尽可能用where先过滤记录,这样因为结果集减小,将对聚合的效率大大提高,最后再根据逻辑看是否用having进行再过滤。
    mysql> select id,count(*) from t group by id;
+------+----------+
| id   | count(*) |
+------+----------+
|    1 |        1 |
|    2 |        2 |
|    3 |        3 |
|    4 |        4 |
+------+----------+
4 rows in set (0.00 sec)

mysql> select id,count(*) from t group by id with rollup;
+------+----------+
| id   | count(*) |
+------+----------+
|    1 |        1 |
|    2 |        2 |
|    3 |        3 |
|    4 |        4 |
| NULL |       10 |
+------+----------+
5 rows in set (0.00 sec)

mysql> select id,count(*) from t group by id having count(1)>2;
+------+----------+
| id   | count(*) |
+------+----------+
|    3 |        3 |
|    4 |        4 |
+------+----------+
2 rows in set (0.08 sec)
    select ename,deptname from emp,dept where emp.deptno=dept.deptno;
    select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
    select ename,deptname from dept right join emp on dept.deptno=emp.deptno;
    select * from emp where deptno in (select deptno from dept);
    select * from emp where deptno = (select deptno from dept limit 1);
    union 和union all的主要区别是union all是把结果集直接合并在一起,而union是将union all后的结果进行一次distinct,去除重复记录后的结果。
    表连接在很多情况下用于优化子查询。    

“mysql基础知识有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI