关于数据库:MySQL-基本语法

2次阅读

共计 3931 个字符,预计需要花费 10 分钟才能阅读完成。

SQL 的概念

什么是 SQL:Structured Query Language 结构化查询语言

SQL 作用:通过 SQL 语句咱们能够不便的操作数据库中的数据库、表、数据。SQL 是数据库管理系统都须要遵循的标准。不同的数据库生产厂商都反对 SQL 语句,但都有特有内容。

SQL 语言的分类:

  • DDL 语句操作数据库以及表的 create,drop,alter 等
  • DML 语句对表数据进行 insert,delete,update
  • DQL 语句对表数据进行各种维度 select 查问
  • DCL 数据管制语言,用来定义数据库的拜访权限和安全级别,及创立用户。关键字 grant,revoke 等

MySQL 数据库的束缚保证数据的正确性、有效性和完整性,包含:主键束缚,惟一束缚,非空束缚

DDL 语法

操作数据库

  1. 显示所有数据库:

    show databases;
  2. 显示数据库:

    show database <database_name>;
  3. 显示创立的数据库信息:

    show create database <database_name>;
  4. 创立数据库:

    create database <database_name>;
  5. 判断数据库是否存在,并创立数据库:

    create database if not exists <database_name>;
  6. 创立数据库并指定字符集:

    create database <database_name> character set <utf8>;
  7. 应用数据:

    use <database_name>;
  8. 查看应用数据库:

    select database();
  9. 批改数据库字符集:

    alter database <database_name> default character set <utf8>;
  10. 删除数据库:

    drop database <database_name>;

操作数据表

  1. 查看所有表:

    show tables;
  2. 创立表:

    create table <table_name> (<name1> <type1>, <name2> <type2>);
  3. 查看表构造:

    desc <table_name>;
  4. 查看建表语句:

    show create table <table_name>;
  5. 创立一个表构造雷同的表:

    create table <new_table_name> like <old_table_name>;
  6. 删除表:

    drop table <table_name>;
  7. 判断表存在并删除表:

    drop table if exists <table_name>;
  8. 增加表列:

    alter table <table_name> add <col_name> <type>;
  9. 批改表列类型:

    alter table <table_name> modify <col_name> <type>;
  10. 批改列名:

    alter table <table_name> change <old_col_name> <new_col_name> <type>;
  11. 删除列:

    alter table <table_name> drop <col_name>;
  12. 批改表名:

    rename table <old_table_name> to <new_table_name>;
  13. 批改表字符集:

    alter table <table_name> character set <utf8>;

DML 语句

  1. 插入全副数据:

    • 值与字段必须对应,个数雷同,类型雷同
    • 值的数据大小必须在字段的长度范畴内
    • 除了数值类型外,其余的字段类型的值必须应用引号(单引号)
    • 如果要插入空值,能够不写字段,或者插入 null

      insert into <table_name>(name1, name2, ...) values(vaule1, value2, ...);
      -- 等价于
      insert into values(vaule1, value2, ...);
  2. 蠕虫复制:

    • 如果只想复制 student 表中 user_name, age 字段数据到 student2 表中应用上面格局

      insert into student2(user_name, age) select user_name, age from student;
      insert into student2() select * from student;
  3. 更新表记录

    • 不带条件批改数据:

      update <table_name> set <name>=<value>;
    • 带条件批改数据:

      update <table_name> set <name>=<value> where <name>=<value>;
  4. 删除表记录

    • 不带条件删除数据:
    delete from <table_name>;
    • 带条件删除数据:
    delete from <table_name> where <name>=<value>;
    • 删除数据

      • delete 是将表中的数据一条一条删除
      • truncate 是将整个表捣毁,从新创立一个新的表,新的表构造和原来的表构造截然不同
      • 主键自增,delete auto_increment 不重置,truncate auto_increment 重置为 1
    truncate table <table_name>;

DQL 语句

查问不会对数据库中的数据进行批改,只是一种显示数据的形式。

  1. 查问值:

    select * from student;
  2. 别名查问:ps: as 可省略不写。

    select <old_col_name> as <new_col_name> from student;
  3. 查问 name,age 后果不呈现反复的 name:

    select distinct name, age from student;
  4. 查问后果参加运算:ps: 参加运算的必须是数值类型。

    select <col_name> + 固定值 from <table_name>
    select <col1_name> + <col2_name> from <table_name>
  5. 查问 id 为 1、3、5 的数据:

    select * from <table_name> where id = 1 or id = 3 or id = 5;
    
    -- 等于
    
    select * from <table_name> where id in (1, 3, 5);
  6. 查问 id 不为 1、3、5 的数据:

    select * from <table_name> where id not in (1, 3, 5);
  7. 查问 id 在 3 到 7 之间的数据:(闭合区间)

    select * from <table_name> where id between 3 and 7;
  8. 含糊查问:

    • %: 示意 0 个或者多个字符(任意字符)
    • \_: 示意一个字符

      select * from <table_name> where <name> like <'通配符字符串'>;
  9. 排序:

    • asc: 升序(默认)
    • desc: 降序

      select * from <table_name> order by age desc, id asc;
  10. 聚合函数

    • count: 统计指定列记录数,记录为 NULL 的不统计。ps: 用 * 可计算所有不为 NULL 的列
    • sum: 计算指定列的数值和,如果不是数据类型,计算结果为 0
    • max: 计算指定列的最大值
    • min: 计算指定列的最小值
    • avg: 计算指定列的平均值,如果不是数值类型,那么计计算结果为 0

      select count(<col_name>) from <table_name>
  11. 分组查问,ps: 个别两个 name 是一样的

    select count(*),<name> from <table_name> group by <name>;

    分组查问后,在进行筛选

    having 和 where 的区别

    • having 是在分组后对数据进行过滤,where 是在分组前对数据进行过滤
    • having 前面能够应用聚合函数,where 前面不能够应用聚合函数
    select count(*),<name> from <table_name> group by <name> having count(*) > 2;
  12. 限度: limit offset length

    • offset: 偏移量,能够认为是跳过的数量,默认 0
    • length: 须要显示几条数据。

      select * from <table_name> limit 3,6

程序:

select *| 字段名 [as 别名] from 表名 [where 子句] [group by 子句] [having 子句] [order by 子句] [limit 子句]

数据库束缚

保证数据的正确性、有效性、完整性。

束缚品种:

  • primary key:主键
  • unique:惟一
  • not null:非空
  • default:默认
  • foreign key:外键

主键

作用:用来惟一标识一条记录,每个表应该有一个主键,并且每个表只能有一个主键。

通过不必业务字段作为主键,独自给每张表设计一个 id 字段,把 id 作为主键。主键是给数据库和程序员应用的,不是给最终客户应用的。主键有没有含意没有关系,只有不反复,非空就行。

create table <table_name> (
   id int primary key,
   name varchar(20)
);

删除主键

alter table <table_name> drop primary key;

主键自增

create table <table_name> (
   id int primary key auto_increment,
   name varchar(20)
);

批改主键自增默认值

alter table <table_name> auto_increment = 100;

惟一束缚

不能插入雷同名字,然而能够插入两个 null

create table <table_name> (
   id int,
   name varchar(20) unique
)

非空束缚

create table <table_name> (
   id int,
   name varchar(20) not null,
   gender char(2)
)

默认设定

create table <table_name> (
   id int,
   name varchar(20),
   location varchar(50) default "射手"
)

更多参考:https://github.com/astak16/bl…

正文完
 0