美文网首页
MySQL常用语句

MySQL常用语句

作者: Bre_eze | 来源:发表于2018-07-03 12:10 被阅读0次

    创建数据库

    create database database-name;
    

    删除数据库

    drop database database-name;
    

    修改数据库名

    rename database database-name-old to database-name-new;
    

    创建新表

    create table if not exist tablename(
     col1-name type1 [not null] [primary key] [auto_increment],
    ...
    )
    

    根据旧表创建新表

    create table table_name_new like table_name_old
    
    create table table_name_new as select col1,col2... from table_name_old;
    

    删除表

    drop table table_name;
    

    修改表的名字

    alter table table_name_old rename table_name_new;
    

    增加一个列

    alter table table_name add column col type [not null]...;
    

    删除一个列

    alter table table_name drop column col_name;
    

    修改列的名称/修改列的属性

    alter table table_name change column col_name_old col_name_new type [not null]...;
    

    选择

    select * from table_name where ...;
    

    插入

    insert into table_name(col1_name, col2_name, ... ) values( ... );
    

    拷贝表中的所有数据

    insert into table_name_1(
    table_name1.col_name1, 
    table_name2.col_name2,
    ...
    )
    select 
    table_name2.col_name1,
    table_name2.col_name2,
    ...
    from table_name2;
    

    跨数据库之间的表的拷贝

    insert into table_name1(a, b, c) 
    select d, e, f 
    from table_name2 in 'database_name' 
    where ... 
    

    删除

    delete from table_name where ...
    

    更新

    update table_name
    set col_name1 = value1,
    ...
    where
    ...
    

    排序(asc升序 desc降序)

    select * from table_name order by col_name1 [asc]/[desc];

    相关文章

      网友评论

          本文标题:MySQL常用语句

          本文链接:https://www.haomeiwen.com/subject/zxwnyftx.html