Mysql数据库基础知识

1. DDL : Data Ddefinition Language  数据定义语言,用来定义数据库对象(数据库、表、字段)

        1.1 数据库操作:

                show databases;

                select database();

                user databaseName;
                create database [if  not exists] databaseName;

                drop database [if exists] databaseName;

        1.2 表操作:

                create table(
                        字段1 字段类型 [约束]  [comment 字段1注释],
                        字段2 字段类型 [约束]  [comment 字段2注释]

                        .....

                )  [comment 表注释] 

                约束:

                        非空约束  not null

                        唯一约束 unique

                        主键约束 primary key(auto increment自增)
                        默认约束 default

                        外键约束 foreign key

                alter table 表名 add 字段名 字段类型(长度) [comment 注释] [约束];

                alter table 表名 modify 字段名  新字段类型(长度);

                alter table  表名 change 旧字段名  新字段名 新字段类型(长度)[comment 注释][约束];       

                droop table 表名 drop colume 字段名;

                rename table 表名 to 新表名;

                

                

 2.DML:Data Manipulation language 数据操作语言 用来对数据库中表的数据记录进行增删改操作
        2.1 Insert
                insert into 表(字段1,字段2 .... ) values (值1,值2.....)

                insert into 表 values(值1,值2....)

                insert into 表  (字段1,字段2 ) values (值1,值2) , (值1,值2) , (值1,值2) 
                insert into 表 values  (值1,值2.....), (值1,值2.....), (值1,值2.....)
        2.2 Update

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

        2.3 Delete

                delete from 表 [where 条件];

3. DQL :Date Query Language 数据查询语言 用来查询数据库中表的记录 

        3.1 基本查询

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

                select  * from 表名;
                select 字段1 [as 别名1] ,字段2 [as 别名2]  from 表名;
                select distinct 字段列表 from 表名;

        3.2 条件查询

                select 字段列表 from 表 where 条件列表

        3.3 分组查询

                聚合函数:将一列数据作为一个整体,进行纵向计算。

                select 聚合函数(字段列表) from  表名;

                null值不参与所有聚合函数运算。

                统计数量:count(*)推荐, count(字段), count(常量) 

                分组查询:

                        select 字段列表 from 表 [where 条件] group by 分组字段名 having 分组后过滤条件

                排序查询:

                        select 字段列表 from 表 [where 条件] group by 分组字段名 order by 字段1  排序方式1 ,字段2  排序方式2

                分页查询:

                        select 字段列表 from 表 limit 起始索引,查询记录数

                内连接
                        式内连接: select 字段列表 from 表1,表2 where 条件…;
                        显式内连接: select 字段列表 from 表1 [ inner ] join 表2 on 连接条件…;
                外连接
                        左外连接: select 字段列表 from 表1 left [ outer ] join 表2 on 连接条件…;
                        右外连接: selec t字段列表 from 表1 right [ outer ] join 表2 on 连接条件…;
                子查询
                        介绍:SQL语句中嵌套select语句,称为嵌套查询,又称子查询。
                        形式: select * from t1 where column1 = ( select column1 from t2 …);
                        子查询外部的语句可以是insert / update / delete / select的任何一个,最常见的是select

4. DCL :Data Control Language 数据控制语言 用来创建数据库用户、控制数据库的访问权限

        4.1 用户管理

        select * from user;

        create user  '用户名'@‘ip地址’ identified by '密码'

        create user '用户名'@‘%’ identified by '密码'

        drop user '用户名'@‘IP地址’

        alter user '用户名'@‘IP地址’ identified with myswl_native_password  by '新密码'

        

        4.2 权限控制

                所有权限:all 或 all privileges

                查询数据:select

                插入数据:insert        

                修改数据:update

                删除数据:delete        

                创建数据库/表:create

                修改表:alter

                删除数据库/表/试图:drop

                show grants for '用户名'@‘ip地址’

                grant 权限列表 on  数据库名.表名 To '用户名'@‘ip地址’

                revoke 权限列表 on 数据库.表名 from '用户名'@‘ip地址’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值