SQL语句概述SQL语言是Structured Query Language的缩写,即结构化查询语言是关系型数据库的标准语言用于维护管理数据库,如数据查询、数据更新、访问控制、对象管理等功能SQL分类

(1)DDL:数据定义语言
(2)DML:数据操纵语言
(3)DQL:数据查询语言
(4)DCL:数据控制语言

DDL操作命令1、DDL语句用于创建数据库对象,如库、表、索引等2、使用DDL语句新建库、表

(1)创建数据库:creste databaes 数据库名
(2)创建数据表:create table 表名 (字段定义……)

3、使用DDL语句删除库、表

(1)删除指定的数据表:drop table [数据库名]表名
(2)删除指定的数据库:drop database 数据库名

DML操作命令1、DML语句用于对表中的数据进行管理2、包括以下操作

(1)insert:插入新数据
(2)update:更新原有数据
(3)delete:删除不需要的数据

3、向数据表中插入新的数据记录

insert into 表名(字段1,字段2, .....) values(字段1的值,字段的值, .....)

4、修改、更新数据表P F的数据记录

update 表名 set 字段名1=值1[,字段名2=值2] where 条件表达式

5、在数据表中删除指定的数据记录

(1)delete from 表名 where 条件表达式
(2)不带where条件的语句表示删除表中所有记录(谨慎操作)

DQL操作命令1、DQL是数据查询语句,只有一条: SELECT2、用于从数据表中查找符合条件的数据记录3、查询时可不指定条件

selext 字段名1,字段名2..... from 表名;

4、查询时指定条件

select 字段名1,字段名2.... from 表名 where 条件表达式;

DCL语句操作1、设置用户权限(用户不存在时,则新建用户)

GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY‘密码’ ]

2、查看用户的权限

SHOW GRANTS FOR 用户名@来源地址

3、撤销用户的权限

REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址

操作实例1、查看数据库列表信息

mysql> show databases; //查看数据库列表信息+--------------------+| Database |+--------------------+| information_schema || mysql | //其中mysql为系统数据库| performance_schema || sys |+--------------------+4 rows in set (0.02 sec)2、创建数据库

mysql> create database school; //创建数据库schoolQuery OK, 1 row affected (0.02 sec)mysql> show databases; //查看数据库列表信息+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || school | //成功创建数据库| sys |+--------------------+5 rows in set (0.00 sec)mysql> 3、创建表

mysql> mysql> use school; //使用数据库schoolDatabase changedmysql> show tables;Empty set (0.00 sec)mysql> create table info ( -> id int(4) not null, -> name char(10) not null, -> address varchar(50) default 'beijing', -> score decimal, -> primary key(id)); //创建表infoQuery OK, 0 rows affected (0.01 sec)mysql> describe info; //查看表结构+---------+---------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+---------+---------------+------+-----+---------+-------+| id | int(4) | NO | PRI | NULL | || name | char(10) | NO | | NULL | || address | varchar(50) | YES | | beijing | || score | decimal(10,0) | YES | | NULL | |+---------+---------------+------+-----+---------+-------+4 rows in set (0.03 sec)4、表中添加数据与查看表中数据

mysql> mysql> insert into info (id,name,address,score) values (1,'stu01','shanghai',88); //添加数据Query OK, 1 row affected (0.03 sec)mysql> insert into info (id,name,address,score) values (2,'stu02','nanjing',79); //添加数据Query OK, 1 row affected (0.01 sec)mysql> insert into info (id,name,address,score) values (3,'stu03',default,90); //添加数据Query OK, 1 row affected (0.00 sec)mysql> insert into info (id,name,address,score) values (4,'stu04','',60); //添加数据Query OK, 1 row affected (0.00 sec)mysql> select * from info; //查看info表中数据+----+-------+----------+-------+| id | name | address | score |+----+-------+----------+-------+| 1 | stu01 | shanghai | 88 || 2 | stu02 | nanjing | 79 || 3 | stu03 | beijing | 90 || 4 | stu04 | | 60 |+----+-------+----------+-------+4 rows in set (0.00 sec)5、修改与删除表中数据

mysql> update info set address='hangzhou' where id=4 and name='stu04'; //修改id为4的address为“hangzhou”Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from info; //查看表中数据+----+-------+----------+-------+| id | name | address | score |+----+-------+----------+-------+| 1 | stu01 | shanghai | 88 || 2 | stu02 | nanjing | 79 || 3 | stu03 | beijing | 90 || 4 | stu04 | hangzhou | 60 |+----+-------+----------+-------+4 rows in set (0.01 sec)mysql> delete from info where name='stu04'; //删除表中name为“stu04”的数据Query OK, 1 row affected (0.02 sec)mysql> select * from info; //查看表中数据+----+-------+----------+-------+| id | name | address | score |+----+-------+----------+-------+| 1 | stu01 | shanghai | 88 || 2 | stu02 | nanjing | 79 || 3 | stu03 | beijing | 90 |+----+-------+----------+-------+3 rows in set (0.00 sec)6、删除表和数据库

mysql> drop table info; //删除表infoQuery OK, 0 rows affected (0.06 sec)mysql> show tables; //查看表,删除成功Empty set (0.00 sec)mysql> drop database school; //删除数据库Query OK, 0 rows affected (0.04 sec)mysql> show databases; //查看数据库,删除成功+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)7、设置、查看与撤销用户权限

mysql> show grants for 'root'@'%'; //查看权限+-------------------------------------------------------------+| Grants for root@% |+-------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |+-------------------------------------------------------------+1 row in set (0.00 sec)mysql> revoke all on *.* from 'root'@'%'; //删除权限Query OK, 0 rows affected (0.03 sec)mysql> show grants for 'root'@'%'; //查看权限+----------------------------------------------------+| Grants for root@% |+----------------------------------------------------+| GRANT USAGE ON *.* TO 'root'@'%' WITH GRANT OPTION |+----------------------------------------------------+1 row in set (0.00 sec)mysql> grant all on *.* to root@'%' identified by 'abc123'; //添加权限Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show grants for 'root'@'%'; //查看权限+-------------------------------------------------------------+| Grants for root@% |+-------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |+-------------------------------------------------------------+1 row in set (0.00 sec)mysql>