MySQL常用语法总结

第一部分:关于MySQL服务

1.安装服务:mysqld --install MySQL-Service --defaults-file=C:\WINDOWS\my.ini

2启动MySQL数据库 net start MySQL-Service
3.停止MySQL服务:net stop MySQL-Service
4.删除MySQLfuwu:sc delete MySQL-Service 或者mysqld --remove(首先必须停止服务)

第二部分:关于数据库
1.连接数据库:mysql -hlocalhost -uroot -padmin-P3306
2.显示数据库:showdatabases;
3.切换数据库:use数据库名;
4. 建数据库:createdatabase  [if not exists] 数据库名;
5.删除数据库:dropdatabase [if exists] 数据库名;
6.显示表结构:desc 表名; show columns from 表名;
7.数据库备份与恢复:
1)mysqldump –u用户名 -p密码 数据库名>文件名  【备份】 . 如mysqldump -uroot -padmin test >"c:data.sql"
2)mysqldump  -h主机名 –u用户名–p密码 数据库 表名1 表名2>文件名【备份】
3)mysql –h 主机名 –u用户名 –p密码 数据库<文本文件   【恢复】
4)select 语句 intooutfile “c:\\test.txt” 【备份】
5)load data infile‘文件路径’ into table 表名 【恢复】注:表须存在

第三部分:关于表的操作:
1.Alert语法
ALTER [IGNORE] TABLE tbl_name
    alter_specification [, alter_specification] ...

alter_specification:
    ADD [COLUMN] column_definition [FIRST | AFTER col_name ]
  | ADD [COLUMN] (column_definition,...)
  | ADD INDEX [index_name] [index_type] (index_col_name,...)
  | ADD [CONSTRAINT [symbol]]
        PRIMARY KEY [index_type] (index_col_name,...)
  | ADD [CONSTRAINT [symbol]]
        UNIQUE [index_name] [index_type] (index_col_name,...)
  | ADD [FULLTEXT|SPATIAL] [index_name] (index_col_name,...)
  | ADD [CONSTRAINT [symbol]]
        FOREIGN KEY [index_name] (index_col_name,...)
        [reference_definition]
  | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
  | CHANGE [COLUMN] old_col_name column_definition
        [FIRST|AFTER col_name]
  | MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
  | DROP [COLUMN] col_name
  | DROP PRIMARY KEY
  | DROP INDEX index_name
  | DROP FOREIGN KEY fk_symbol
  | DISABLE KEYS
  | ENABLE KEYS
  | RENAME [TO] new_tbl_name
  | ORDER BY col_name
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]
  | DISCARD TABLESPACE
  | IMPORT TABLESPACE
  | table_options
  | partition_options
  | ADD PARTITION partition_definition
  | DROP PARTITION partition_names
  | COALESCE PARTITION number
  | REORGANIZE PARTITION partition_names INTO (partition_definitions)
  | ANALYZE PARTITION partition_names
  | CHECK PARTITION partition_names
  | OPTIMIZE PARTITION partition_names
  | REBUILD PARTITION partition_names
  | REPAIR PARTITION partition_names

   2.Create语法
    ;创建表
   CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    [(create_definition,...)]
    [table_options] [select_statement]
;创建索引
   CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
    [USING index_type]
    ON tbl_name (index_col_name,...)
   3.Drop语法
    ;删除表
    DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]
    ;删除索引
    DROP INDEX index_name ON tbl_name
4.Delete语法
    
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [WHERE where_definition]
    [ORDER BY ...]
    [LIMIT row_count]
 5.Insert语法
    INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    VALUES ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

你可能感兴趣的:(MySQL)