一、启动和关闭mysql服务

 
(方法3)在计算机——服务——mysql,通过图形界面关闭

 
(方法2)终端下执行 service mysql start (这种方法需要RPM方式安装的mysql才行)

 
(方法2)service stop mysql (这种方法需要RPM方式安装的mysql才行)

 
(3)重启mysql

二、 连接数据库

mysql -u root -p,回车后输入密码,不写 -h 表示使用本机ip地址。

三、 SQL语句分类

 
数据控制语句,用于控制不同数据段直接的许可和访问级别的语句。这些语句定义了数据库、表、字段、用户的访问权限和安全级别,主要的语句关键字包含grant、revoke等。

;,然后回车,才会被执行。

四、 DDL语句操作

 
(d)test: 系统自动创建的测试数据库,任何用户都可以使用

show tables;,显示该数据库下的所有表。

status,显示当前connection的信息,比如使用的数据库、用户、数据库的字符集等。

 
在mysql中,drop语句的操作结果均显示'0 rows affected'

 

drop table tbname;

alter table tbname rename new_name

五、 DML语句

 
(valk1, valk2, ... valkn)

 
比如 udpate emp a, dept b set a.sal=b.sal*b.deptno, b.deptname=a.ename where a.deptno=b.deptno;#emp表(记为a)和dept表(记为b),a中存放雇员信息,包括雇员薪水和其所在的部门号;b中存放部门信息,包括部门号和部门名称。对于给出a的一条雇员信息记录,通过其部门号deptno,从b中找到该deptno对应的deptname.....

 
无论是单表还是多表,如果不加where条件,会将表的所有记录删除。

 
排序规则默认为asc(升序排序)

 
[group by field1, field2....fieldn] [with rollup] [having where_condition]

 
having和where的区别:having是对聚合后的结果进行条件的过滤,而where是在聚合前就对记录进行过滤,如果逻辑允许,尽可能使用where先过滤记录,这样减少结果集,提高效率。

 
select max(val), min(val), sum(val) from emp; 统计所有的雇员中的最大、最小、总薪水值

 
select ename, deptname from emp left join dept on emp.deptno=dept.deptno;emp表左连接dept表,连接后的结果中包含emp表中的所有的ename,即使该ename对应的员工的deptno在dept表中不存在。

 
转化为表连接之后,为 select emp.* from emp, dept where emp.deptno=dept.deptno;

 

    union和union all的区别在于:union all是把结果集直接合并在一起,而union是将union all的结果进行一次distinct,去除重复记录后的结果。

 
select deptno from emp union all select deptno from dept;这样将显示所有的在emp表和deptno表中出现的deptno,去掉重复:select deptno from emp union select deptno from dept;

六、DCL语句

 
revoke insert on sakila.* from 'zl'@'localhost';

七、 查询元数据信息

 
statistics:该表提供了关于表索引的信息

 
select concat('drop table test1.', table_name, ';') from tables where table_schema='test1' and table_name like 'tmp%';

相关文章:

  • 2021-04-12
  • 2022-02-13
  • 2022-01-17
  • 2021-10-27
  • 2021-11-26
  • 2021-07-04
猜你喜欢
  • 2021-12-23
  • 2021-09-10
  • 2022-12-23
  • 2021-11-28
相关资源
相似解决方案