zoukankan      html  css  js  c++  java
  • mysql 基本操作

    今天介绍以下简单的sql语指令,数据库,表,数据行的增删改查操作。

    首先是数据库的增删改查:

              增   create databases   databases指的是数据库名称

              例子  create database  db1  创建名为db1的数据库

              删    drop database db1     删除数据库,在工作中是严禁使用这个指令的

              改  对于数据库而言并没有修改的方法,如果真的有修改需要就重新在建一个数据库

              查 show databases     查看数据库

    数据表 :

              在创建数据表的时候要先指定在一个数据库下 使用 use 加数据库名称即可,

                                     新建数据表:create table 加上表名括号后面加上列名 加上列名的列类型,列类型是必须要加的。

              在指定类型时为了防止在插叙时出现编码格式不一致而导致的乱码问题,要统一以下字符编码可以统一使用utf8来进行编码,也可以自己去配置文件来建立全局的编码格式这样就不用,每次都进行制定了。

              create table 表名(t1)() 列名(id 类型(int)auto_increment primary key)这里为了防止id号有重复的现象发生 使用自增来生成id编号,在使用自增时必须使用 peimary key是为了在查找时可以更加快速的定位到我们想要的数据位置。

         

    2. 数据表:

    新建:
    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;


    列类型:

    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
    无符号: 0 到 255 unsigned
    smallint
    范围:
    有符号: -32768到32767
    无符号: 0 到 65535 unsigned

    mediumint
    范围:
    有符号: -8388608到8388607
    无符号: 0 到 16777215 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



    指令:
    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);


    3. 数据行:


    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;


    七. 外键: (*******************************************************************)

    缺点:
    1. 数据重复
    2. 如果 部门过长的话, 太占用空间


    解决方法:

    重新设计一张表, 这张表 中存放部门的相关信息


    部门表:

    create table department (
    id int auto_increment primary key,
    depart_name varchar(32) not null default ''
    )engine=Innodb charset=utf8;

    insert into department (depart_name) values ('公关'), ('关关'),('关公');

    create table userinfo (
    id int auto_increment primary key,
    name varchar(32) not null default '',
    depart_id int not null default 1,

    # constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
    constraint fk_userinfo_depart foreign key (depart_id) references department(id)

    )engine=Innodb charset=utf8;


    insert into userinfo (name, depart_id) values ('root1', 1);
    insert into userinfo (name, depart_id) values ('root2', 2); 错误的

    注意:
    创建多个外键的时候, 名称不能一样

    =====> 一对多

  • 相关阅读:
    一致性哈希算法
    Discourse 的标签(Tag)只能是小写的原因
    JIRA 链接 bitbucket 提示错误 Invalid OAuth credentials
    JIRA 如何连接到云平台的 bitbucket
    Apache Druid 能够支持即席查询
    如何在 Discourse 中配置使用 GitHub 登录和创建用户
    Apache Druid 是什么
    Xshell 如何导入 PuTTYgen 生成的 key
    windows下配置Nginx支持php
    laravel连接数据库提示mysql_connect() :Connection refused...
  • 原文地址:https://www.cnblogs.com/1624413646hxy/p/11013388.html
Copyright © 2011-2022 走看看