zoukankan      html  css  js  c++  java
  • mysql基础知识

    在这里面所有的命令都有分号 ; 结束
    连接到数据库
    mysql -uroot -ppassword [-hlocalhost];
    中括号中的内容代表该内容是可选的。当只输入-h的时候代表默认连接本机的数据库

    退出数据库
    exit
    quit
    ctrl + c

    简单的显示当前时间的函数命令
    select now();
    mysql中的函数相当于Java中的方法
    输出当前数据库的版本
    select version();
    显示这个数据库管理系统中有多少个数据库
    show databases;
    使用某个数据库
    use databasename;
    显示这个数据库中有多少个表
    show tables;
    查看一个表的结构
    desc tablename;
    创建一个数据库
    create database databasename;
    删除一个表
    drop table tablename;
    创建一个表
    create table tablename
    (字段1,类型,长度,
    字段2,类型,长度,
    字段3,类型,长度,
    字段4,类型,长度);
    插入数据
    insert into tablename
    (字段1,字段2,字段3,。。。)
    values
    (value1,value2,value3。。。);
    在插入数据的时候要求字段类型,顺序,数量要与值的相互一致。
    查询语句
    select from tablename;
    在创建表的的时候声明主键
    create table tablename
    (字段1,类型,长度,
    字段2,类型,长度,
    .................
    primary key(id));
    主键名一般情况下为ID,主键值不能为null,主键与业务,代码无关,不更改,不重用。任意两行都不具备相同的主键值。任意一行都要有主键。主键可以是一列或多个列的组合,使用primary key(id)来声明主键。
    当时联合主键的时候这样声明primary key(xxx,xxx);

    设置主键自动增长
    auto_increment;
    只有主键可以自动增长,自动增长的列一定是主键列
    字段值不能为null
    not null;
    字段值不能重复
    unique;
    主键默认值
    default (default value);

    更改表
    添加一列
    alter table tablename add column_name type length;
    删除一列
    alter table tablename drop column column_name;
    添加主键约束
    alter table tablename add constraint pri_id primary key(id);
    添加unique约束
    alter table tablename add uq_column_name unique(column_name);
    设置默认约束
    alter table tablename alter column column_name set default value;
    设置非null约束
    alter table tablename modify column columnname type length not null;
    重命名表
    rename table tablenameone to tablenametwo;
    备份在DOS窗口中执行
    mysqldump -hipaddress -uroot -ppassword databasename>D:/backup.sql;
    批量导入在mysql环境中执行
    source D:/backup.sql;

    常用的数据类型
    tinyint
    smallint
    int
    bigint
    char
    varchar
    tinytext
    text
    mediumtext
    longtext
    date
    time
    datetime
    timestamp
    float
    double
    decimal(m,d)

  • 相关阅读:
    LeetCode——Generate Parentheses
    LeetCode——Best Time to Buy and Sell Stock IV
    LeetCode——Best Time to Buy and Sell Stock III
    LeetCode——Best Time to Buy and Sell Stock
    LeetCode——Find Minimum in Rotated Sorted Array
    Mahout实现基于用户的协同过滤算法
    使用Java对文件进行解压缩
    LeetCode——Convert Sorted Array to Binary Search Tree
    LeetCode——Missing Number
    LeetCode——Integer to Roman
  • 原文地址:https://www.cnblogs.com/shininguang/p/4848328.html
Copyright © 2011-2022 走看看