page contents

MySQL数据库基本语句,通通拿走

MySQL 数据库基本操作数据库操作创建数据库:createdatabase 数据库名显示数据库:showdatabases;删除数据库:dropdatabase 数据库名 ;数据库操作实践mysql> show databases;+...

attachments-2021-04-F168D4Mi607d17fd69d9f.png

MySQL 数据库基本操作

数据库操作


  • 创建数据库:

    createdatabase 数据库名

  • 显示数据库:

    showdatabases;

  • 删除数据库:

  • dropdatabase 数据库名 ;


    数据库操作实践

    mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || sys                |+--------------------+4 rows in set (0.02 sec)
    mysql> create database test;Query OK, 1 row affected (0.00 sec)
    mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || test |+--------------------+5 rows in set (0.00 sec)
    mysql> drop database test;Query OK, 0 rows affected (0.00 sec)
    mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)

创建、修改和删除表


  • 创建表

    create table 表名 (  属性名 数据类型 [完整性约束条件]    属性名 数据类型 [完整性约束条件]    属性名 数据类型 [完整性约束条件]    属性名 数据类型);


例如:

create table example(    id int,    name varchar(20),    sex boolean);

  • 查看表结构

    describe 表名;

  • 查看表定义

    describe

  • 查看表详细结构语句

    showcreatetable 表名;


  • 修改表

    • 修改表名
      alter table 旧表名 rename 新表名;
      例:alter table example rename newexample;


    • 修改字段的数据类型
      alter table 表名 modify 属性名 数据类型;
      例:alter table user modify name varchar(30);


    • 修改字段名
      alter table 表名 change 旧属性名 新属性名 新数据类型;


    • 增加字段
      alter table 表名 add 属性名1 数据类型  [完整性约束条件] [first| after 属性名2];例:alter table user add phone varchar(20);


    • 删除字段

      alter table 表名 drop 属性名;例:alter table user drop id;


    • 删除表

      drop table 表名;例:drop table example2;


SQL查询语句


  • 基本查询语句

    slelect 属性列表    from 表名和识图列表  [where 条件表达式1]    [group by 属性名1 [having 条件表达式2]]    [order by 属性名2 [ase|dese]]例:select num,name,age,sex,homeaddr from employee;


  • 单表查询

    • 查询所有字段

      在MySQL中select语句的“属性列表”中可以为*。

      select * from 表名;


    • 查询指定记录

      where 条件表达式;例:select * from employee where id=1001;


    • where查询条件


    • 比较

      =、<=、!=、<>(不等于,等价于!=)、!>、!<等
      指定范围

      between and 、not between and
      指定集合

      in 、not in
      匹配字符

      like 、not like
      是否为空值


      is null、 is not null
      多个查询条件

      and 、or
      select * from employee where id in (1001,1005);select * from employee where name not in ('张三','李四');select * from employee where age between 15 and 25;select * from employee where age not between 15 and 25;select * from employee where name like 'Aric';select * from employee where name = 'Aric';//同上select * from employee where homeaddr like '北京%';select * from employee where homeaddr ='北京%';//同上select * from employee where name not like '张%';select * from work where info is null;    //使用IS NULL 关键字查询work表中info字段为空值的记录
    • 带and的多条件查询

      select *from employee where    id =1001 and sex like '男'


    • 带or的多条件查询

      select *from employee where    id =1001 or sex like '男'


    • 对查询结果排序

      order by 属性名 [ASC|DESC]例:select *from employee order by age;


    • 分组查询

      group by 属性名 [having 条件表达式][with rollup]


    • 用limit限制查询结果的数量

      select * from employee limit 2;//只显示前两条查询结果select * from employee limit 0,2;//同上select * from employee limit 12


  • 使用集合函数查询

    • count()函数

      select count(*) from employee;//查询employee表的记录数


    • sum()函数

      select num,sun(score) from grade where num = 1001;select num,sun(score) from grade group by  num;


    • avg()函数

      select avg(age) from employee;//平均数


    • max()、min()函数

      select max(age) from employee;select min(age) from employee;


 目前就这些,需要再进行添补。


相关文章:

SQL建立数据库:创建和使用数据库

SQL语句的21个好习惯,建议收藏

MySQL高级性能优化实战!看这篇就够了

SQL数据库查询语句基本语法:常用的数据查询语句

更多技术资讯,请继续关注六星社区-程序员编程技术分享交流学习高端论坛

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-06-0IQRXKXk62a1afa92e9ad.jpeg

  • 发表于 2021-04-19 13:59
  • 阅读 ( 716 )
  • 分类:数据库

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1320 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章