0
点赞
收藏
分享

微信扫一扫

MySQL数据库进阶系统学习2(Shell命令脚本)


第二部分:MySQL-Shell命令脚本

5. MySQL数据库的命令行脚本

  • MySQL的安装:参考Python爬虫开发和项目实战pdf中的注释和QQ浏览器中数据库中的MySQL里面有安装收藏
  • Navicat:参考我的博客和QQ浏览器中数据库中的Navicat里面有收藏
  • MySQL启动方式:
  • 方式1:打开CMD窗口执行:mysql -u root -p 然后输入MySQL的密码00----56
  • 方式2:开始菜单里面有一个MySQL 8.0 Command Line Client打开即可
  • MySQL shell命令:
  • SQL语句不区分大小写,语句可以多行写,最终结尾是分号‘;’
  • 有了分号,按下enter才会执行

5.1. 数据库操作

  • 连接数据库:
    mysql -u root -p 接着输入密码
    mysql -u root -p密码
  • 退出数据库:
    exit\quit\ctrl+d
  • 退出当前输入状态:
    \c
  • 查看版本:
    select version();
  • 显示当前时间:
    select now();
  • 显示所有数据库:(注意后面分号要写)
    show databases;
  • 使用数据库
    use 数据库名;
  • 查看当前使用的数据库
    select database();
  • 创建数据库
    create database 数据库名 charset=utf8;
    例:
    create database python charset=utf8;
  • 显示数据库是怎么创建的:
    show create database python;
  • 删除数据库
    drop database 数据库名;
    例:
    drop database python;

5.2. 数据表操作

  • 查看当前数据库中所有表
    show tables;
  • 查看表结构
    desc 表名;
  • 创建表
    auto_increment表示自动增长
    CREATE TABLE table_name(
    column1 datatype contrai,
    column2 datatype,
    column3 datatype,

    columnN datatype,
    PRIMARY KEY(one or more columns)
    );
    例:创建班级表
    create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(10)
    );
    例:创建学生表
    create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default ‘’,
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum(‘男’,‘女’,‘人妖’,‘保密’),
    cls_id int unsigned default 0
    )
  • 修改表-添加字段
    alter table 表名 add 列名 类型;
    例:
    alter table students add birthday datetime;
  • 修改表-修改字段:重命名版
    alter table 表名 change 原名 新名 类型及约束;
    例:
    alter table students change birthday birth datetime not null;
  • 修改表-修改字段:不重命名版
    alter table 表名 modify 列名 类型及约束;
    例:
    alter table students modify birth date not null;
  • 修改表-删除字段
    alter table 表名 drop 列名;
    例:
    alter table students drop birthday;
  • 删除表
    drop table 表名;
    例:
    drop table students;
  • 查看表的创建语句
    show create table 表名;
    例:
    show create table classes;
  • 查看表中具体内容
    select * from students;

5.3. 增删改查(curd)

curd的解释: 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)

5.3.1. 查询基本使用

  • 查询所有列
    select * from 表名;
    例:
    select * from classes;
  • 查询指定列
    可以使用as为列或表指定别名
    select 列1,列2,… from 表名;
    例:
    select id,name from classes;

5.3.2. 增加

  • 格式:INSERT [INTO] tb_name [(col_name,…)] {VALUES | VALUE} ({expr | DEFAULT},…),(…),…
  • 说明:主键列是自动增长,但是在全列插入时需要占位,通常使用0或者 default 或者 null 来占位,插入成功后以实际数据为准
  • 全列插入:值的顺序与表中字段的顺序对应
    insert into 表名 values(…)
    例:
    insert into students values(0,’郭靖‘,1,‘蒙古’,‘2016-1-2’);
  • 部分列插入:值的顺序与给出的列顺序对应
    insert into 表名(列1,…) values(值1,…)
    例:
    insert into students(name,hometown,birthday) values(‘黄蓉’,‘桃花岛’,‘2016-3-2’);
  • 上面的语句一次可以向表中插入一行数据,还可以一次性插入多行数据,这样可以减少与数据库的通信
  • 全列多行插入:值的顺序与给出的列顺序对应
    insert into 表名 values(…),(…)…;
    例:
    insert into classes values(0,‘python1’),(0,‘python2’);
    insert into 表名(列1,…) values(值1,…),(值1,…)…;
    例:
    insert into students(name) values(‘杨康’),(‘杨过’),(‘小龙女’);

5.3.3. 修改删除

  • 修改
  • 格式: UPDATE tbname SET col1={expr1|DEFAULT} [,col2={expr2|default}]…[where 条件判断]
    update 表名 set 列1=值1,列2=值2… where 条件
    例:
    update students set gender=0,hometown=‘北京’ where id=5;
  • 删除
  • DELETE FROM tbname [where 条件判断]
    真正的删除:
    delete from 表名 where 条件
    例:
    delete from students where id=5;
    实际使用逻辑删除:
    逻辑删除,本质就是修改操作
    update students set isdelete=1 where id=1;
    delete from students; 删除students这个数据表
  • 逻辑删除常规操作
  • 先添加is_delete一列,作为标记列(默认值为0):删除的标记为1,未删除的标记为0,
  • mysql> alter table students add is_delete bit default 0;
  • mysql> select * from students;
    ±—±-----±-----±-------±-------±-------±--------------------±----------+
    | id | name | age | height | gender | cls_id | birthday | is_delete |
    ±—±-----±-----±-------±-------±-------±--------------------±----------+
    | 1 | 老王 | 18 | 188.80 | 男 | 0 | 0000-00-00 00:00:00 | |
    | 2 | 小李 | 20 | 180.00 | 男 | 1 | 1990-01-01 00:00:00 | |
    | 3 | 小李 | 35 | 180.00 | 女 | 1 | 1990-01-01 00:00:00 | |
    ±—±-----±-----±-------±-------±-------±--------------------±----------+
  • 上面查看已经添加了一列,然后执行逻辑删除:
  • mysql> update students set is_delete=1 where id=2;
  • mysql> select * from students;
    ±—±-----±-----±-------±-------±-------±--------------------±----------+
    | id | name | age | height | gender | cls_id | birthday | is_delete |
    ±—±-----±-----±-------±-------±-------±--------------------±----------+
    | 1 | 老王 | 18 | 188.80 | 男 | 0 | 0000-00-00 00:00:00 | |
    | 2 | 小李 | 20 | 180.00 | 男 | 1 | 1990-01-01 00:00:00 | |
    | 3 | 小李 | 35 | 180.00 | 女 | 1 | 1990-01-01 00:00:00 | |
    ±—±-----±-----±-------±-------±-------±--------------------±----------+
  • 逻辑删除后,数据有了一个特殊标记
  • 查看未注销的:select * from students where is_delete=0;
  • 查看已经注销的:select * from students where is_delete=1;


举报

相关推荐

0 条评论