Mysql使用入门(一)sql语句分类

来源:互联网 发布:淘宝的中国质造 编辑:程序博客网 时间:2024/05/21 17:25

Mysql使用入门(一)sql语句分类

目录

  • Mysql使用入门一sql语句分类
    • DDL语句
    • DML语句
    • DCL语句
    • 其他实用语句总结

sql语句主要划分为以下三个类别:

  • DDL(Data Definition Language)语句:数据定义语句
  • DML(Data Manipulation Language)语句:数据操纵语句
  • DCL(Data Control Language)语句:数据控制语句

DDL语句

这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象的定义。

常用语句 含义 create datebase dbname 创建数据库dbname drop datebase dbname 删除dbname数据库 create table table1(name_1 type_1 constraints,name_2 type_2 constraints) 创建table1表,name为列的名字,type为列的数据类型,constraints为列的约束条件 drop table table1 删除table1表 alter table table1 drop column name_1 将字段name_1删掉 alter table table1 rename new_table1 将表table1改名为new_table1

DML语句

这些语句主要用于添加、删除、更新和查询数据库记录,并检查数据完整性

常用语句 含义 insert into table1 (field1,field2…fieldn) values (‘value1’,’value2’…’valuen’) 向table1表插入记录 update table1 set field1=value1,field2=value2…fieldn=valuen[where condition] 修改表table1表中的记录值 delete from table1 where field1=value1 在table1表中将所有field1=value1的记录删除 select * from table1 查询table1表中的所有记录 select * from table1 where field1=value1 条件查询,查询所有field1=value1的记录 select * from table1 order by field1 将表table1中的记录按照field1的值从大到小排列显示 select * from table1 limit 3 查询table1表中的前3条记录 select * from table1 order by field1 将表table1中的记录按照field1的值从大到小排列显示 select * from table1 where field1=(select field1 from table2) 子查询 select * from table1 union select * from table2 将tabel1和table2中的结果查询出来合并到一起显示出来

DCL语句

这些语句定义了数据库、表、字段、用户的访问权限和安全级别。

常用语句 含义 grant select,insert on database.* to ‘user1’@’localhost’ identified by ‘123’ 创建一个数据库用户user1,具有对database数据库中所有的select/insert权限 remoke insert on database.* from ‘user1’@’localhost’ 收回user1的insert权限

其他实用语句总结

常用比较实用的语句 含义 mysql -uroot -p 启动mysql服务之后,该命令用于连接到mysql服务器。mysql表示客户端命令;-u后面跟连接的数据库用户;-p表示需要输入密码 use dbname 选择数据库dbname show tables 查看数据库中创建的所有表 desc table1 查看表table1的定义
0 0