zoukankan      html  css  js  c++  java
  • 数据库基础

    Linux连接所需数据:用户名,密码,连接地址,端口

    MySQL连接所需数据:用户名,密码,连接地址,端口

    重启数据库

    service mariadb restart

    连接数据库

    mysql -uroot -p

    mysql -uroot -hlocalhost -P3306 -p
        -u:用户名
        -h:连接地址(不写默认为本地)
        -P:端口号(不写默认为3306)

    数据库软件/系统

    一块地

    数据库/库

    一个个的仓库

    一个个的货架(更好地分类)

    数据

    一件件货物

    帮助

    help 关键词

    例如:help create database

    sql语句

    命令结束后要在后面加上一个英文分号作为结束符;

    列出所有的库

    show databases;

    创建数据库

    create database 库名;

    例子:
    create database if not exists test20170807
    default character set utf8
    default collate utf8_general_ci;
    新建一个叫做test20170807的库,在新建前判断该库名是否存在,不存在则新建,否则当啥事都没发生
    设置默认字符集为utf8(支持中文)
    设置默认字符规则为collate utf8_general_ci(不区分大小写)

    进入库

    use 库名;

    查看所有表

    show tables;

    查看某个表的格子

    desc 表名;

    设置主键

    primary key

    第一种:
     create table test(
     id int primary key
     );

    第二种:
     create table test(
     id int,
     primary key(id)
     );

    第三种:
     alter table test add primary key(id);

    第四种:
     alter table test modify id int primary key;

    删除主键

    alter table test drop primary key;

    PS:一张表里面只能有一个主键
        主键对应的字段,所有的值都是唯一的

    设置自动增长

    auto_increment

    第一种:
     create table test(
     id int primary key auto_increment
     );

    第二种:
     alter table test modify id int auto_increment;

    PS:
      一张表里面只能有一个自动增长,并且自动增长的字段      必须为主键,当自动增长存在时,主键无法单独移除
      自动增长默认为当没有给到相应的值或者值为空时,会自动给字段填充一个数字,具体为比上一条多1(相对于自动增长的值来说,默认最开始为1)

    空值

    null,“”

    修改表字段

    alter table 表名 modify 字段名 字段类型 [其他附件参数];

    alter table 表名 change 字段名 字段名 字段类型 [其他附件参数];

    alter table 表名 add 字段名 字段类型 [其他附件参数];

    alter table 表名 drop 字段名;

    查询语句

    select 字段名 from 表名 [条件];(已有选择的默认库/已经进入到对应库)
    例子:select host,user,password from user;

    select 字段名 from 库名.表名 [条件];(没有默认库/当前所在库不是即将查询表的库)
    例子:select host,user,password from mysql.user;

    插入语句

    insert into 表名[字段名] values(插入的值);

    insert into 表名[字段名] values(插入的值),(插入的值);

    PS:
    如果表名后面没有指定字段名,那么插入的值必须从一个字段开始,依次输入

    修改语句

    update 表名 set 字段=值 [条件]

    删除语句

    delete from 表名 [条件]

    删除delete

    删除表里面的数据

    删除drop

    删除库、表、表字段

    条件

    where 字段=值
     =号为逻辑符号,其他还有>、<

  • 相关阅读:
    python中可变类型和不可变类型
    python PEP8开发规范
    pandas之——Series常用总结
    python os 模块的使用
    Markdown语法
    HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查
    MySQL union all排序问题
    mysql解决datetime与timestamp精确到毫秒的问题
    keepalived + nginx实现高可用
    配置文件keepalived.conf详解
  • 原文地址:https://www.cnblogs.com/G-MingYin/p/7498173.html
Copyright © 2011-2022 走看看