MySQL语法学习
登錄數(shù)據(jù)庫:/mysql/bin/mysql -u root -p
-u:用戶名
-p:密碼
-h:數(shù)據(jù)庫連接IP或域名,默認(rèn)localhost的可省
-P:連接端口,默認(rèn)3306的可省略
DatabaseName:數(shù)據(jù)庫名稱,帶此參數(shù)時(shí),登錄成功后直接轉(zhuǎn)到該數(shù)據(jù)庫下;
輸入登錄用戶的密碼(輸入時(shí)不顯示出來,輸完回車即可):Enter password:
?
例:以root用戶,在域名為localhost,端口為3306上登錄并直接進(jìn)入到mysql數(shù)據(jù)庫
mysql -h localhost -P 3306 -u root -p mysql
?
查詢時(shí)間:select now();
查詢當(dāng)前用戶:select user();
查詢數(shù)據(jù)庫版本:select version();
查詢當(dāng)前使用的數(shù)據(jù)庫:select database();
查詢當(dāng)前日期:select current_date();
查詢從庫同步狀態(tài):show slave status\G;
查詢數(shù)據(jù)庫進(jìn)程情況:show processlist;
?
列出數(shù)據(jù)庫:mysql> show databases;
?
選擇數(shù)據(jù)庫:use DatabaseName;
例:mysql> use mysql; ?使用mysql數(shù)據(jù)庫
?
列出表格:mysql> show tables;
?
顯示表格列的屬性:show columns from tableName;
例:mysql> show columns from user; ?顯示user表格列的屬性
?
創(chuàng)建數(shù)據(jù)庫:create database DatabaseName;
例:mysql> create database mytest; ?使用mytest數(shù)據(jù)庫
?
刪除數(shù)據(jù)庫:drop database if exists DatabaseName;
例:mysql> drop database if exists mytest; ?刪除mytest數(shù)據(jù)庫
?
創(chuàng)建數(shù)據(jù)表:create table TableName(字段設(shè)定列表);
例:mysql> create table user(
`id` tinyint(8) not null auto_increment primary key,
`name` varchar(32) not null default '',
`password` varchar(32) not null default '',
`sex` enum('0', '1') not null default '0')ENGINE=MyISAM;
lock tables user write;
insert user(`name`, `password`, `sex`) values ('testNmae', '1234', '0'),('testNmae', '12345', '1'),
('testNmae', '123456', '0'),('testNmae', '12345678', '1');
unlock tables;
創(chuàng)建了一個(gè)表名為user表,含有id,name,password,sex字段;
?
刪除數(shù)據(jù)表:drop table if exists TableName;
例:mysql> drop table if exists user; ?刪除user數(shù)據(jù)表
?
清空數(shù)據(jù)表:truncate table TableName;
例:mysql> truncate table user; ?清空user數(shù)據(jù)表
?
優(yōu)化數(shù)據(jù)表:optimize table TableName;
例:mysql> optimize table user; ?優(yōu)化user數(shù)據(jù)表
?
修復(fù)數(shù)據(jù)表:repair table TableName;
例:mysql> repair table user; ?修復(fù)user數(shù)據(jù)表
?
分析數(shù)據(jù)表:analyze table TableName;
例:mysql> analyze table user; ?分析user數(shù)據(jù)表
?
檢查數(shù)據(jù)表:check table TableName;
例:mysql> check table user; ?檢查user數(shù)據(jù)表
?
數(shù)據(jù)表添加列(字段):alter table TableName add (字段設(shè)定列表) [ after ColumnName ];
例:alter table user add `e_mail` varchar(255) not null default '' after id ?在user數(shù)據(jù)表里插入一個(gè)新的字段e_mail位于字段id之后
?
數(shù)據(jù)表更改列(字段):alter table TableName change 舊列名 (新字段設(shè)定列表) [ after ColumnName ];
例:alter table user change `e_mail` `eMail` varchar(255) not null default '' after id; ?在user數(shù)據(jù)表里將字段為e_mail修改為eMail并更新位置?
?
數(shù)據(jù)表刪除列(字段):alter tables TableName drop column ColumnName, ColumnName2;
例:alter table user drop column `eMail`; ?在user數(shù)據(jù)表里將字段為eMail的列刪除?
?
導(dǎo)出一個(gè)完整數(shù)據(jù)庫:
登錄數(shù)據(jù)庫: /mysql/bin/mysqldump -u root -p DatabaseName > OutFileName
-u:用戶名
-p:密碼
-h:數(shù)據(jù)庫連接IP或域名,默認(rèn)localhost的可省
-P:連接端口,默認(rèn)3306的可省
DatabaseName:要導(dǎo)出的數(shù)據(jù)庫名稱;
>:輸出到指定文件
OutFileName:存放導(dǎo)出數(shù)據(jù)庫的文件
?
導(dǎo)出一個(gè)數(shù)據(jù)庫中的指定表(一個(gè)或多個(gè)):
/mysql/bin/mysqldump -u root -p DatabaseName DataTableName1 DataTableName2 > OutFileName
-u:用戶名
-p:密碼
-h:數(shù)據(jù)庫連接IP或域名,默認(rèn)localhost的可省
-P:連接端口,默認(rèn)3306的可省
DatabaseName:要導(dǎo)出的數(shù)據(jù)庫名稱;
>:輸出到指定文件
DataTableName1:要導(dǎo)出的一個(gè)表名
DataTableName2:要導(dǎo)出的另一個(gè)表名
...有多個(gè)表時(shí),以空格形式書寫即可導(dǎo)出多個(gè)
OutFileName:存放導(dǎo)出數(shù)據(jù)庫的文件
?
grant 權(quán)限1,權(quán)限2,…權(quán)限n on 數(shù)據(jù)庫名稱.表名稱 to 用戶名@用戶地址 identified by ‘連接口令’;
控制到庫級(jí)的命令示范:
grant all privileges on mytest.* to shangcheng@'localhost' identified by '123456';
用戶名為:shangcheng
庫名:shangcheng
密碼:123456
?
刷新系統(tǒng)權(quán)限表:mysql> flush privileges;?
?
修改用戶密碼:set password for 'user'@'host' = PASSWORD('newpassword');
例:set password for 'root'@'localhost' = PASSWORD('123456'); ?將登錄方式為localhost的root用戶修改密碼為123456
?
查看用戶權(quán)限:show grants for 'user'@'host';
例:show grants for 'root'@'localhost'; ?查看登錄方式為localhost的root用戶的權(quán)限
轉(zhuǎn)載于:https://blog.51cto.com/chinatree/521745
總結(jié)
- 上一篇: 一条SQL语句查询数据库中的所有表、存储
- 下一篇: 【2011-04-06】SQL Serv