mysql學習

mysql命令:
? 查詢當前的一些命令
mysql> ?
For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
For server side help, type 'help contents'
clear     (\c) 終止的意思
如:
mysql> show databasedsfdsaf \c
delimiter (\d) 設置語句結束的分隔符
如:
mysql> \d //
mysql> show databases;
    -> //
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
mysql> show databases //
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
ego       (\G)  不管你定義的任何的分隔符一定要執行最後結果,並豎排
如:
mysql> show databases \G
*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: mysql
*************************** 3. row ***************************
Database: test
3 rows in set (0.00 sec)
go        (\g) Send command to mysql server
source    (\.) 導入文件 等同於mysql -uroot -pdtsdts >mysql.sql
如:
mysql> source linux.sql
system    (\!)  不用退出mysql也可以查看/etc下面的文件
如:
mysql> \! ls /etc
warnings  (\W) 顯示警告信息
nowarning (\w) 關閉警告信息
mysql也支持命令自動補齊
方法一:
編輯/etc/my.cnf
添加以下內容
[mysql]
no-auto-rehash
方法二:
使用命令:mysql -A  
 mysqladmin -uroot -hlocalhost -predhat ping 檢測mysql是否存活   
   
 ysqladmin -uroot -plinuxsed processlist
複製一個表內容但是結構會丟失:
create tables linuxawk select *from user;
複製一個表結構,不會複製內容
create table tbt5 like tb4;
查看一個表的狀態:
show table status like "user"\G;
修改默認存儲引擎:
default_storage_engine=Innodb
create語法:
create table tb3(ID smallint unsigned not null auto_increment primary key,Name char(30) not null,AGE tinyint unsigned,SALARY float(10,2)not null );
mysql> desc tb3;
+--------+----------------------+------+-----+---------+----------------+
| Field  | Type                 | Null | Key | Default | Extra          |
+--------+----------------------+------+-----+---------+----------------+
| ID     | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| Name   | char(30)             | NO   |     | NULL    |                |
| AGE    | tinyint(3) unsigned  | YES  |     | NULL    |                |
| SALARY | float(10,2)          | NO   |     | NULL    |                |
+--------+----------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> insert into tb3(name,age,salary)values("Jerry",23,23333.50);
Query OK, 1 row affected (0.06 sec)
mysql> select *from tb3
    -> ;
+----+-------+------+----------+
| ID | Name  | AGE  | SALARY   |
+----+-------+------+----------+
|  1 | Jerry |   23 | 23333.50 |
+----+-------+------+----------+
1 row in set (0.00 sec)
mysql> create table tb4(ID smallint unsigned not null auto_increment primary key ,Name char(50)not null,GENDER char(1)not null default 'M',BDATE date not null)engine=myisam charset=utf8;
Query OK, 0 rows affected (0.11 sec)
數據庫定義語言:DDL
create drop alter
查看當前mysql用戶的權限
mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*CEFE9CF698C1B1FA9D0B010DCD146963187D8002' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看指定用戶的權限
mysql> show grants for root@'localhost';
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*CEFE9CF698C1B1FA9D0B010DCD146963187D8002' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看mysql所有用戶的權限
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+---------------------------------------+
| query                                 |
+---------------------------------------+
| User: 'root'@'127.0.0.1';             |
| User: ''@'localhost';                 |
| User: 'root'@'localhost';             |
| User: ''@'localhost.localdomain';     |
| User: 'root'@'localhost.localdomain'; |
+---------------------------------------+
5 rows in set (0.00 sec)
原表
mysql> desc tb4;
+---------+----------------------+------+-----+---------+----------------+
| Field   | Type                 | Null | Key | Default | Extra          |
+---------+----------------------+------+-----+---------+----------------+
| ID      | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| Name    | char(30)             | NO   |     | NULL    |                |
| AGE     | tinyint(3) unsigned  | YES  |     | NULL    |                |
| SALARY  | float(10,2)          | NO   |     | NULL    |                |
| Subject | char(10)             | NO   |     | NULL    |                |
+---------+----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
alter添加一下字段
mysql> alter table tb4 add linuxsed char(10) not null;
Query OK, 1 row affected (0.10 sec)
Records: 1  Duplicates: 0  Warnings: 0
alter刪除一個字段
mysql> alter table tb4 drop linuxsed;
Query OK, 1 row affected (0.09 sec)
Records: 1  Duplicates: 0  Warnings: 0
alter修改字段類型
mysql> alter table tb4 modify age smallint unsigned not null;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0
alter修改字段名稱
mysql> alter table tb4 change age old smallint unsigned not null;
Query OK, 1 row affected (0.08 sec)
Records: 1  Duplicates: 0  Warnings: 0
alter修改表的默認存儲引擎
mysql> alter table tb4 engine='myisam';
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0
alter重命名錶
mysql> alter table tb4 rename  to linuxsed;
Query OK, 0 rows affected (0.00 sec)
DML
truncate 和 delete的效率問題:
如果想要刪除表的所有數據,truncate語句要比 delete 語句快。因爲 truncate 刪除了表,然後根據表結構重新建立它,而 delete 刪除的是記錄,並沒有嘗試去修改表。這也是爲什麼當向一個使用 delete 清空的表插入數據時,MySQL 會記住前面產生的AUTOINCREMENT序列,並且繼續利用它對AUTOINCREMENT字段編號。而truncate刪除表後,表是從1開始爲autoincrement字段編號。
不過truncate命令快規快,卻不像delete命令那樣對事務處理是安全的。因此,如果我們想要執行truncate刪除的表正在進行事務處理,這個命令就會產生退出併產生錯誤信息
alter添加指定字段後一行
 alter table  stu add StuID tinyint not null after ID;
   
 create table classes (ClassID smallint unsigned not null auto_increment primary key ,Class varchar(50) not null,NumofStu tinyint unsigned,Tutor smallint unsigned,CourseID1 smallint unsigned,CourseID2 smallint unsigned,CourseID3 smallint unsigned);
   
   
 insert into classes (Class,NumofStu,Tutor,CourseID1,CourseID2,CourseID3) values ('Class01',36,2,2,4,5),('Class02',41,1,1,3,4),('Class03',85,4,5,3,2);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章