python进阶(九)_mysql基本命令

数据库的操作

  1. 连接数据库
    1. mysql -uroot -p
    2. mysql -uroot -p824750
  2. 退出数据库
    1. quit
    2. exit
    3. ctrl+d
  3. sql语句最后需要有分号;结尾
  4. 显示时间
    select now();
    
  5. 显示版本
    select version();
    
  6. 查看所有数据库
    show databases;
    
  7. 创建数据库
    create database 数据库名 charset=utf8;
    create database python4;
    create database python4new charset=utf8;
    
  8. 查看创建数据库的语句
    show create database 数据库名;
    show create database python4;
    
  9. 查看当前使用的数据库
    select database();
    
  10. 使用数据库
    use 数据库名;
    use python4new;
    
  11. 删除数据库
    drop database 数据库名;
    drop database python4;
    drop database `python-04`;      # ‘为tab上面的键
    

数据表操作

  1. 查看当前数据库中所有的表

    show tables;
    
  2. 创建数据表

    • auto_increment: 表示自动增长
    • not null: 表示不能为空
    • primary key: 表示主键
    • default: 默认值
    • create table: 数据表名(字段 类型 约束[,字段 类型 约束])
     create table xxxxx(id int, name varchar(30));
     create table yyyyy(id int primary key not null auto_increment, name varchar(30));
      create table zzzzz(
         id int primary key not null auto_increment,
         name varchar(30)
         );
    

    创建students表(id name age high gender cls_id)

     --创建students表(id name age high gender cls_id)
     create table students(
         id int unsigned not null auto_increment primary key,
         name varchar(30),
         age tinyint unsigned,
         high decimal(5,2),
         gender enum("男","女性","中性","保密") default "保密",
         cls_id int unsigned
     );
     --创建班级表
     create table classes(
         id int unsigned auto_increment primary key not null,
         name varchar(10)
     );
    
  3. desc 查看数据表结构
    desc 数据表名
    desc xxxxx;

  4. 查看表的创建语句
    show create table 表名

    show create table students
    
  5. 修改表;

    1. 添加字段
      alter table 表名 add 列名 类型及约束;
      alter table students add birthday datetime;
      
    2. 修改字段:不重名版
      alter table 表名 modify 列名 类型及约束;
      alter table students modify birthday date;
      
    3. 修改字段:重命名版
      alter table 表名 change 原列名 新列名 类型及约束;
      alter table students change birthday birth date default "1990--01-01";
      
  6. 删除字段
    alter table 表名 drop 列名;

    alter table students drop high;
    
  7. 删除表
    drop table 表名;
    drop table xxxxx

数据操作

数据的增删改查(crud)

  1. 增加
    全列插入
    insert [into] 表名 values(…)
    主键字段可以用 0 NULL default 来占位

    --向classes表中查入一个班级
    insert into classes values(0,"菜鸟");
    --向students表插入一个学生信息
    +--------+-------------------------------------+------+-----+------------+----------------+
    | Field  | Type                                | Null | Key | Default    | Extra          |
    +--------+-------------------------------------+------+-----+------------+----------------+
    | id     | int(10) unsigned                    | NO   | PRI | NULL       | auto_increment |
    | name   | varchar(30)                         | YES  |     | NULL       |                |
    | age    | tinyint(3) unsigned                 | YES  |     | NULL       |                |
    | high   | decimal(5,2)                        | YES  |     | NULL       |                |
    | gender | enum('男','女','中性','保密')         | YES  |     | 保密       |                |
    | cls_id | int(10) unsigned                    | YES  |     | NULL       |                |
    | birth  | date                                | YES  |     | 1990-01-01 |                |
    +--------+-------------------------------------+------+-----+------------+----------------+
    
    insert into students values(0,"小李飞刀",20,155,"男",2123,default);
    insert into students values(null,"小李飞刀"20155,"男",2123,default);
    insert into students values(default,"小李飞刀",20,155,"男",2123,default);
    insert into students values(0,"小李飞刀",20,155,3,2123,default);
    

    部分插入
    insert into 表名(列1,…) values(值1,)

    insert into students(name,gender) values("小乔",2);
    

    多行插入

      insert into students(name,gender) values(("小乔",2),("大乔",2);
      insert into students values(default,"西施",20,155,2,2123,default),(default,"王昭君",20,155,2,2123,default);
    
  2. 修改
    update 表名 set 列1=值1,列2=值2… where 条件

     update students set gender=1; --全部修改
     update students set gender=1 where name="小李飞刀";
     update students set gender=1 where id=3;
     update students set gender=1,age=22 where id=3;
    
  3. 删除
    物理删除(慎用)
    delete from 表名 where 条件

    delete from students; --删除整个数据表示
    delete from students where name="小李飞刀";
    

    逻辑删除
    用一个字段来表示这条信息是否被已经不能再使用了

    --给students表添加一个is_delete字段 bit类型
    alter table students add is_delete bit default 0;
    update students set is_delete=1 where id=6;
    
  4. 查询的基本用法
    查询所有列
    select * from 表名;

     select * from students;
     select * from students where name="小李飞刀";
     select * from students where id>3;
    

    查询指定列
    select 列1,列2,… from 表名 where …;

     select name,gender from students;
    

    可以使用as为列或表指定别名
    select 字段1[as 别名1],字段2[as 别名2],…表名 where …;

       select name as 姓名,gender as 性别 from students;
    

    字段的顺序
    select 字段2[as 别名2],字段1[as 别名1],…表名 where …;

     select gender as 性别,name as 姓名 from students;
     select id as 序号,gender as 性别,name as 姓名 from students;
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值