• MYSQL常用语句总览


    启动MYSQL

    mysql -uroot -p

    远程连接MYSQL

    mysql -h ip端口号 -u root -p

    数据库操作

    查看数据库

    show databases;

    创建数据库(此处以user数据库为例)

    create database user;

    使用数据库(此处以创建user数据库为例)

    use user;

    表操作

    创建表(此处以创建user表为例)

    create table user(

      id int(10) unsigned not null auto_increment,

      name varchar(25),

      sec varchar(5),

      primary key(id))engine=innodb;

    查看表结构(此处以创建user表为例)

    desc user

    复制表

    在开发过程中可以复制一个新的表作为测试表,而不用操作正式的表,以保证正在运行的数据不被破坏。

    复制表1(复制表结构、数据、主键、索引)

    create table new_table like old_table;

    insert table new_table select * from old_table;

    复制表2(只复制表结构、数据、不复制主键、索引)

    create table new_table select * from old_table;

    复制表3(只复制表结构,不复制数据,主键,索引)

    create table new_table select * from old_table where 0;

    创建临时表(此处以创建user表为例)

    create temporary table user(int id not null)

    创建内存表(此处以创建user表为例)

    create table user(int id not null)ENGINE=MEMORY

    数据操作

    插入

    insert into 表名(字段名,字段名) values (值,值);

    insert into 表名 values(值,值);

    修改

    update 表名 set 字段名=值,字段名=值 where 条件

    删除

    delete from 表名 where 条件

    查询

    select 查询内容 from 表名 where 表达式 group by 字段名 having 表达式 order by 字段名 limit 记录数

    通过正则表达式查询

    select * from user where 字段名 regexp ’正则表达式‘

    表之间的连接查询(此图来自尚硅谷MYSQL笔记资料)

    字段操作

    设置主键

    crate table user(字段描述,primary key(index_col_name1,index_col_name2))

    查看表主键

    show create table user;

    删除表主键

    alter table user drop primary key;

    增加表主键

    alter table user add primary key(id);

    添加字段

    alter table user add phone varchar(25) not null;

    改变字段类型

    alter table user modify phone int(25) not NULL;

    字段重命名

    alter table <表名> change <字段名> <字段新名称> <字段类型>

    字段默认值

    alter table 表名 alter 字段名 set default 默认值;

    删除字段默认值

    alter table 表名 alter 字段名 drop default;

    设置自增字段

    create table user(id int auto_increment,name varchar(255));

  • 相关阅读:
    LeetCode "Super Ugly Number" !
    LeetCode "Count of Smaller Number After Self"
    LeetCode "Binary Tree Vertical Order"
    LeetCode "Sparse Matrix Multiplication"
    LeetCode "Minimum Height Tree" !!
    HackerRank "The Indian Job"
    HackerRank "Poisonous Plants"
    HackerRank "Kundu and Tree" !!
    LeetCode "Best Time to Buy and Sell Stock with Cooldown" !
    HackerRank "AND xor OR"
  • 原文地址:https://www.cnblogs.com/wfswf/p/15746869.html
Copyright © 2020-2023  润新知