• MySQLStudy——数据库 数据表 数据行 增删改查


    数据库(database)的操作 

    create database 数据库名称;
    例子:
    create database db1;

    drop database 数据库名称;
    
    drop database db1;

    没有专门的修改指令 
    
    只能删了重新建

    查询

    show databases;
    
    使用:
    use 数据库名;
    use db1;

    数据表(table)的操作

    新建

    首先进入数据库,才能看到数据表
    use db1;
    版本0:
    SQL语句:
    create table 表名 (
    列名1 列类型
    );
    
    例子:
    create table t1 (
    id int,
    name char(32) 
    );

    指令: 
    insert into 表名 (列1, 列2) values (值1, 值2);
    
    例子: 
    insert into t1 (id, name) values (1, 'zekai');
    insert into t1 (id, name) values (2, '你好');
    
    改进1:
    create table 表名 (
    列名1 列类型
    )engine=Innodb charset=utf8;
    
    ps: 
    引擎: Innodb 和 MyIsam 
    5.5 版本以上 默认是Innodb
    
    create table t2 (
    id int,
    name char(32) 
    )engine=Innodb charset=utf8;
    
    insert into t2 (id, name) values (1, '你好');
    insert into t2 (id, name) values (1, 'xxx');
    
    改进2:
    
    create table 表名 (
    列名1 列类型 auto_increment primary key
    )engine=Innodb charset=utf8;
    
    create table t4 (
    id int auto_increment primary key, 
    name char(32) not null default ''
    )engine=Innodb charset=utf8;
    
    auto_increment : 自增
    primary key : 主键索引 (作用: 加快查找的速度)
    not null : 不能为空
    default : 默认值
    
    注意: 后面一列写完之后, 不能加逗号 (*********)
    
    一种:
    insert into t3 (id, name) values (1, '你好');
    insert into t3 (id, name) values (2, 'xxx');
    二种:
    insert into t3 (name) values ('hello');
    insert into t3 (name) values ('xxx');
    
    
    -------------------------------------------------------------
    
    最终的格式:
    create table 表名 (
    列1 列属性 [是否为null 默认值],
    列2 列属性 [是否为null 默认值],
    .....
    列n 列属性 [是否为null 默认值]
    )engine = 存储引擎 charset = 字符集
    最终的例子:
    create table t4 (
    id int auto_increment primary key, 
    name char(32) not null default '',
    pwd char(32) not null default ''
    )engine=Innodb charset=utf8;

    指令:
    select 列名 from 表名;
    例子:
    select * from t1;

    指令:
    drop table 表名;
    
    连带着将数据表中的所有数据都会删掉
    
    ps: 工作中, 线上数据库, 这个命令根本不会让你用到
    
    实例:
    drop table t1;    

    show tables;
    
    desc 表名; : 查看表的结构
    
    show create table 表名 : 查看表的创建过程
    
    关于主键自增: (不是重点)
    show session variables like 'auto_inc%';
    set session auto_increment_increment = 2;
    
    show global variables like 'auto_inc%';
    set global auto_increment_increment = 2;

    create table t4 (
    id int auto_increment primary key, 
    name char(32) not null default '',
    pwd char(32) not null default ''
    )engine=Innodb charset=utf8;
    
    修改字段:
    alter table 表名(t3) change 原列名(name) 新列名(username varchar(32) not null default '');
    
    新增字段:
    alter table 表名(t3) add 新列(pwd char(32) not null default '');
    
    删除字段:
    alter table 表名(t3) drop 列名(pwd);

    字段类型

    a. 数值型
    create table t4 (
    id unsigned mediumint auto_increment primary key, 
    name char(32) not null default '',
    pwd char(32) not null default ''
    )engine=Innodb charset=utf8;
    
    tinyint : 
    范围: 
    有符号: -128到127
    无符号: 0255 unsigned
    smallint
    范围: 
    有符号: -32768到32767
    无符号: 065535 unsigned
    
    mediumint
    范围: 
    有符号: -8388608到8388607
    无符号: 016777215 unsigned
    int
    bigint
    
    区别: 
    a. 取值范围不一样, 根据自己公司的业务来去选择
    b. 无符号和有符号的意思
    
    float(M,D) 浮点型
    decimal(M,D) 定点型 比float更加的精准
    
    例如: 3.1415151519868789789
    float: 3.141515000000000000
    decimal : 3.1415151519868789789
    
    126.35
    
    M:小数总共多少位 decimal(5, )
    D:小数点后面几位 decimal(5, 2)
    
    使用场景:
    比如 说存 salary 工资 : 6000.23 decimal(,2)
    
    b. 字符串类型
    
    char : 定长 char(32) 这一列的值就是32 优点: 速度快 缺点: 浪费
    varchar : 变长 varchar(32) 优点: 不浪费, 节省空间 缺点: 速度慢
    
    根据自己公司的业务来去选择:
    
    create table userinfo (
    id unsigned mediumint auto_increment primary key, 
    name varchar(128) not null default '',
    pwd char(32) not null default '',
    create_time datetime not null default '1970-01-01 00:00:00'
    )engine=Innodb charset=utf8;
    
    一般情况下, 如果没有100%的把握, 都是用varchar()
    
    
    text: 文本 范围比较大, 如果存储大量字符的话, 可以使用这个字段
    
    c. 时间类型
    date 2019-6-12
    
    推荐使用datetime

    数据行(row data)

    insert into t3 (id, name) values (1, '你好');

    查询

    select * from t3; : 将表中的 所有的列全部列出
    select 列名, 列名, 列名 from t3 : 将某一列的值查出

    delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上一个 ID
    
    truncate 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始
    
    truncate 速度快
    
    ps: 工作中, 线上数据库, 这个命令根本不会让你用到
    
    delete from 表名(t3) where name = 'xxxxx';

    update t3 set username='zekai';
    
    update t3 set username='xxxx' where id=3;
    
    update t3 set username='xxxx', pwd='xxxxx' where id=3;

    总结

    数据库:

    增:
    create database 数据库名;
    删:
    drop database 数据库名;
    查:
    show databases;

    数据表:
    创建:
    create table 表名(
    id int auto_increment primary key
    列名1 列类型 [not null/ null default '' ],
    列名2 列类型 [not null/ null default '' ],
    列名n 列类型 [not null/ null default '' ]
    )engine=Innodb charset=utf8;

    删除:
    drop table 表名;
    查询:
    show tables;

    修改:
    alter table 表名 change/modify 旧列名 新列声明;

    alter table 表名 add 新列声明;

    alter table 表名 drop 旧列名;


    数据行:

    增:
    insert into 表名 (列1,列2, 列n) values (值1, 值2, ..., 值n);

    删除:
    delete from 表名 where id=12;;
    truncate 表名;

    修改:
    update 表名 set name='xxxx';
    update 表名 set name='xxxx' where id=12;
    update 表名 set name='xxxx',age=12 where id=12;

    查询:
    select * from 表名;
    select name, age from 表名;

  • 相关阅读:
    LeetCode 404. 左叶子之和
    三年了
    LeetCode 543. 二叉树的直径
    求结点在二叉排序树中层次的算法
    LeetCode 98. 验证二叉搜索树
    LeetCode 236. 二叉树的最近公共祖先
    LeetCode 129. 求根到叶子节点数字之和
    LeetCode 113. 路径总和 II
    LeetCode 107. 二叉树的层次遍历 II
    LeetCode 144. 二叉树的前序遍历 (非递归)
  • 原文地址:https://www.cnblogs.com/tingguoguoyo/p/11040457.html
Copyright © 2020-2023  润新知