欢迎光临
我们一直在努力

mysql常用sql语句整理

  • 增加一张表
    1
    2
    3
    CREATE TABLE `table_name`(
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 增加记录
    1
    2
    3
    4
    INSERT INTO `your_table_name`(`column_name`)
    VALUES
    (‘your_value_one’),
    (‘your_value_two’);
  • 增加字段
    1
    2
    3
    ALTER TABLE `your_table_name`
    ADD `your_column_name` …
    AFTER `column_name`;
  • 增加索引
    • 主键
      1
      2
      ALTER TABLE `your_table_name`
      ADD PRIMARY KEY your_index_name(your_column_name);
    • 唯一索引
      1
      2
      ALTER TABLE `your_table_name`
      ADD UNIQUE your_index_name(your_column_name);
    • 普通索引
      1
      2
      ALTER TABLE `your_table_name`
      ADD INDEX your_index_name(your_column_name);
    • 全文索引
      1
      2
      ALTER TABLE `your_table_name`
      ADD FULLTEXT your_index_name(your_column_name);

  • 逐行删除
    1
    2
    DELETE FORM `table_name`
    WHERE …;
  • 清空整张表
    1
    TRUNCATE TABLE `your_table_name`;
  • 删除表
    1
    DROP TABLE `your_table_name`;
  • 删除字段
    1
    2
    ALTER TABLE `your_table_name`
    DROP `column_name`;
  • 删除索引
    1
    2
    ALTER TABLE `your_table_name`
    DROP INDEX your_index_name(your_column_name);

  • 变更数据
    1
    2
    3
    UPDATE `table_name`
    SET column_name=your_value
    WHERE …;
  • 变更字段
    1
    2
    ALTER TABLE `your_table_name`
    CHANGE `your_column_name` `your_column_name` …(变更);
  • 变更字段值为另一张表的某个值
    1
    2
    3
    4
    5
    6
    UPDATE `your_table_name`
    AS a
    JOIN `your_another_table_name`
    AS b
    SET a.column = b.another_column
    WHERE a.id = b.a_id…;

  • 普通查询
    1
    2
    SELECT `column_name_one`, `column_name_two`
    FROM `table_name`;
  • 关联查询
    1
    2
    3
    4
    5
    6
    SELECT *
    FROM `your_table_name`
    AS a
    JOIN `your_anther_table_name`
    AS b
    WHERE a.column_name = b.column_name…;
  • 合计函数条件查询:WHERE 关键字无法与合计函数一起使用
    1
    2
    3
    4
    SELECT aggregate_function(column_name)
    FROM your_table_name
    GROUP BY column_name
    HAVING aggregate_function(column_name)…;
  • 同一个实例下跨库查询
    1
    2
    3
    4
    5
    6
    SELECT *
    FROM database_name.your_table_name
    AS a
    JOIN anther_database_name.your_anther_table_name
    AS b
    WHERE a.column_name = b.column_name…;

复制一张表结构

1
2
CREATE `your_table_name`
LIKE `destination_table_name`;

完全复制一张表:表结构+全部数据

1
2
3
4
5
6
CREATE `your_table_name`
LIKE `destination_table_name`;
INSERT INTO `your_table_name`
SELECT *
FROM `destination_table_name`;

附录:mysql常用命令

  • 登陆: mysql -h host -u username -p
  • 列出数据库:SHOW DATABESES;
  • 列出表:SHOW TABLES;
  • 表结构:DESC table_name;
  • 使用一个数据库:USE database_name;
  • 导入:source ‘file’;
  • 导出:mysqldump -h 127.0.0.1 -u root -p “database_name” “table_name” –where=”condition” > file_name.sql;

未经允许不得转载:阿神的博客 » mysql常用sql语句整理

分享到:更多 ()