SQL语句 (mysql)

SQL主要分为三类:

DDL:数据定义语句  数据定义语言  定义不同的数据段,数据表、列、索引等数据库对象 『create drop alter』

DML :数据操作语句  数据操作语句,用于添加、删除、更新和查询数据库记录 『insert ,delete,update,select』

DCL:数据控制语句    数据控制语句,用于控制不同数据段之间的许可和访问级别的语句,『grant,revoke』

[root@host50 mysql]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.    ------语句以;或者\g结束
Your MySQL connection id is 5                                       -----------------连接mysql的次数
Server version: 5.7.17 MySQL Community Server (GPL)                   --------------------mysql的版本信息

DDL语句

mysql> create database zhuhaiyan;       ----------创建名称叫zhuhaiyan的数据库
Query OK, 1 row affected (0.01 sec)
mysql> drop database zhuhaiyan;          -----------删除名称叫zhuhaiyan的数据库
Query OK, 0 rows affected (0.00 sec)

mysql> create table zhy(name char(4),age int(2));     -----------创建表名称为zhy的表,表中的字段为name,age
Query OK, 0 rows affected (0.72 sec)

mysql> alter table zhy add column sex char(2);            --------修改表:向表名为zhy的表插入了字段为sex的字段
Query OK, 0 rows affected (0.47 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table zhy drop column age;                     ---------修改表:在表名为zhy删除字段为age的列
Query OK, 0 rows affected (0.46 sec)
Records: 0  Duplicates: 0  Warnings: 0
 

mysql> alter table zhy add age int(2)  after name;            -------修改表:在表中添加age的字段,并追加到name字段后
Query OK, 0 rows affected (1.02 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> alter table zhy modify age int(2) first;  -----修改表:把age字段放在第一列
Query OK, 0 rows affected (0.62 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table zhy modify name char(6);    -----修改表中的字段的宽度

mysql> drop table zhy ;                                 -------删除名为zhy的表
Query OK, 0 rows affected (0.11 sec)
mysql> alter user root@"localhost" identified by "123456";    -------修改用户的密码
Query OK, 0 rows affected (0.00 sec)

mysql> alter table 学生信息表 rename student;                  -----更加表的名称
Query OK, 0 rows affected (0.22 sec)
 

mysql> alter table zhy change sex sex1 char(3);               --------修改表的的字段名,把字段为sex改成sex1
Query OK, 0 rows affected (0.91 sec)
Records: 0  Duplicates: 0  Warnings: 0

DML语句

mysql> insert into zhy value(11,'zhu','nv');        ---------向表zhy中插入数据
Query OK, 1 row affected (0.04 sec)

mysql> insert into zhy(age,name,sex1) value(12,'hai','man');      ---------向表zhy中插入数据
Query OK, 1 row affected (0.05 sec)

mysql> insert into zhy(age,name) values(13,'hz'),(15,'ss');         ---------向表zhy中插入数据
Query OK, 2 rows affected (0.10 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> update zhy set name='yan' where age=12;       ---------单表更新数据
Query OK, 2 rows affected (0.09 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno;   ---------单表更新数据

mysql> delete a,b  from emp a,dept b where a.deptno=b.deptno and a.deptno=3;    ---------删除表中的数据
Query OK, 2 rows affected (0.13 sec)

mysql> select distinct deptno from emp;                         -------查询表中的数据,加distinct是表示不显示重复数据
2 rows in set (0.00 sec)

mysql> select * from emp where deptno>1;                    -------条件查找,值大于1的则显示
1 row in set (0.00 sec)

mysql> select * from emp order by deptno;                -------排序查询,默认是升序查询

4 rows in set (0.00 sec)

mysql> select * from emp order by deptno desc;            ---------排序查询,降序查询

4 rows in set (0.00 sec)


mysql> select * from emp order by sa1 limit 2;             -------只显示前两行

mysql> select * from emp order by sa1 limit 1,2;           -----显示1行后的2行

mysql> select count(1) from emp;                                    --------统计emp的行数

mysql> select deptno,count(1) from emp group by deptno;        -------在部门的条件统计人数

mysql> select deptno,count(1) from emp group by deptno with rollup;    -----统计部门人数和总人数

mysql> select deptno,count(1) from emp group by deptno having count(1)>1;    ----统计部门人数大于1

mysql> select sum(sa1),max(sa1),min(sa1) from emp;   ----统计公司所有员工的薪水、最高和最底薪水

select ename,deptname from dept right join emp on dept.deptno=emp.deptno;     -----右连接查询

select ename,deptname from emp left join dept on dept.deptno=emp.deptno;         ------左连接查询

mysql> select * from emp where deptno in(select deptno from dept);                 ------子查询
mysql> select deptno from emp union  select deptno from dept;                        ------联合查询(union all 显示重复的数)
 

DCL语句

mysql> grant select,insert on zhuhaiyan.* to 'zhu'@'localhost' identified by '654321';    
Query OK, 0 rows affected, 1 warning (0.00 sec)

                          -------将数据库zhuhaiyan的查询和插入权限给用户zhu,用户zhu的登录权限为654321

mysql> revoke select on zhuhaiyan.* from 'zhu'@'localhost';
Query OK, 0 rows affected (0.00 sec)                        

                            -------将数据库zhuhaiyan的查询权限从用户zhu收回

猜你喜欢

转载自blog.csdn.net/zhydream77/article/details/81154957
今日推荐