• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Juliet吖
博客园    首页    新随笔    联系   管理    订阅  订阅
MySQL一系列

一、Mysql

# 1.登录数据库客户端
mysql -uroot -p
Enter password:******

# 2.查看所有库
show databases;

# 3.新建一个库
create database 库名 charset utf8;

# 4.进入数据库
use 库名;

# 5.查看所有表
show tables;

# 6.创建一张表
create table 表名(字段名 类型;字段名 类型,.....);

# 7.查看一下表结构
desc 表名;

# 8.查看当前在哪个数据库下
select database();

# 9.删除表
drop table表名;

# 10.删除库
drop database 库名;

二、Mysql表操作

1.1常用数据类型:

 

  • 整数:int    bit

 

  • 小数:decimal
  • 字符串:varchar,  char
  • 日期时间: date, time, datetime
  • 枚举类型(enum)

1.2约束

  • 主键(primary  key):物理上存储的顺序 MySQL建议所有表的主键都叫id字段,类型为int  unsigned
  • 非空(not null):不允许填写空值
  • 唯一(unique):值不允许重复
  • 默认(default):当不填写字段对应的值会使用默认值,如果填写时以填写为准
  • 外键(foreign  key):对关系字段进行约束,当为关系字段填写值时,会到关联的表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并抛出异常

2.1表结构的操作

2.1.1创建表

example:
    create  table  subject(
            ->id  int  primary  key,
            ->subname  varchar(32)
            ->);

    create table student(
            ->id int primary key,
            ->name varchar(32),
            ->age int not null,
            ->snum varchar(32) unique,
            ->gender int default 1,
            ->sub_id int,
            ->foreign key(sub_id) references subject(id)
            ->);

2.1.2修改表

alter  table  表名   add  列名  类型;

2.2数据添加

1)全列插入
insert  into  表名   values (……);

2)部分列插入

insert   into  表名   (列1,……)  values(值1,……);

3)全列多行插入
insert   into  表名  values(……),(……);

4)部分列多行插入
insert   into  表名(列1,……)  values(值1,……),(值2,...)...;

2.3数据修改

update  表名  set   列1=值1,列2=值2...  where  条件

2.4数据删除

delete   from   表名   where   条件
注意:上面的操作称之为物理删除,一旦删除就不容易恢复。

逻辑删除,本质就是修改操作

例:
update  student  set   isdelete=1  where  id=1;

3.数据查询

  • 查询所有字段
select  *  from  表名
  • 查询指定字段
select 列1,列2,... from 表名;
例:
  select name,age from students

4. 条件

4.1条件查询语法:

select * from 表名 where 条件;
select * from students where id=1;

4.2  where后面支持多种运算符,进行条件的处理

  • 比较运算符

  • 逻辑运算符

  • 模糊查询

  • 范围查询

  • 空判断

4.3比较运算符

  • 等于: =

  • 大于: >

  • 大于等于: >=

  • 小于: <

  • 小于等于: <=

  • 不等于: != 或 <>

例1:查询编号大于3的学生

select * from students where id > 3;

例2:查询编号不大于4的学生

select * from students where id <= 4;

例3:查询姓名不是“黄蓉”的学生

select * from students where name != '黄蓉';

例4:查询没被删除的学生

select * from students where is_delete=0;

4.4逻辑运算符

  • and

  • or

  • not

例5:查询编号大于3的女同学

select * from students where id > 3 and gender=0;

例6:查询编号小于4或没被删除的学生

select * from students where id < 4 or is_delete=0;

优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符

and比or先运算,如果同时出现并希望先算or,需要结合()使用

4.5 模糊查询

  • like

  • %表示任意多个任意字符

  • _表示一个任意字符

例7:查询姓黄的学生

select * from students where name like '黄%';

例8:查询姓黄并且“名”是一个字的学生

select * from students where name like '黄_';

例9:查询姓黄或叫靖的学生

select * from students where name like '黄%' or name like '%靖';

4.6 范围查询

  • in表示在一个非连续的范围内

例10:查询编号是1或3或8的学生

select * from students where id in(1,3,8);
  • between ... and ...表示在一个连续的范围内

例11:查询编号为3至8的学生

select * from students where id between 3 and 8;

例12:查询编号是3至8的男生

select * from students where (id between 3 and 8) and gender=1;

注意: between A and B在匹配数据的时候匹配的范围空间是 [A,B]

4.7 空判断

  • 注意:null与''是不同的

  • 判空is null

例13:查询没有填写身高的学生

select * from students where height is null;
  • 判非空is not null

例14:查询填写了身高的学生

select * from students where height is not null;

例15:查询填写了身高的男生

select * from students where height is not null and gender=1;

4.8 排序的使用

排序查询语法:select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]

 

语法说明

* 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
* 默认按照列值从小到大排列(asc)
* asc从小到大排列,即升序
* desc从大到小排序,即降序

 

posted on 2021-06-03 10:32  Juliet吖  阅读(49)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3