MySql 复习SQL基础
1. 顯示當前存在的數據庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.01 sec)2. 創建數據庫
create database student; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | student | +--------------------+ 4 rows in set (0.00 sec)3. 進入數據庫
mysql> use student; Database changed4. 創建表
mysql> create table information(id int, name text, serial text, address text); Query OK, 0 rows affected (0.01 sec)5. 顯示所有表
mysql> show tables; +-------------------+ | Tables_in_student | +-------------------+ | information | +-------------------+ 1 row in set (0.01 sec)5. 列舉表中詳細字段
mysql> desc information; +---------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | text | YES | | NULL | | | serial | text | YES | | NULL | | | address | text | YES | | NULL | | +---------+---------+------+-----+---------+-------+ 4 rows in set (0.00 sec)6. 數據插入表中
mysql> insert into information values(1,"wk","123","nmg"); Query OK, 1 row affected (0.00 sec)7. 查看表信息
mysql> select * from information; +------+----------+--------+---------+ | id | name | serial | address | +------+----------+--------+---------+ | 1 | wk | 123 | nmg | | 1 | zhaoyun | 123 | xj | | 1 | zhangfei | 321 | sd | +------+----------+--------+---------+ 3 rows in set (0.00 sec) select * form information where name != "ll";當前日期:select date;
升序:select * form information order by id;
降序:select * form information order by id desc;
查詢數學成績最高的5名:select top 5 * from TblScore order by tMath desc
內聯:如果想把用戶信息、積分、等級都列出來,那么一般會這樣寫:select * from T1 ,T3 where T1.userid = T3.userid(其實這? ? ? ? ? ? 樣的結果等同于select * from T1 inner join T3 on T1.userid=T3.userid )。
? ? ? ? ? 把兩個表中都存在userid的行拼成一行(即內聯),但后者的效率會比前者高很多,建議用后者(內聯)的寫法。
| 運行結果 | T1.userid | username | password | T2.userid | jifen | dengji |
| ? | 1 | jack | jackpwd | 1 | 20 | 3 |
左聯:顯示左表T1中的所有行,并把右表T2中符合條件加到左表T1中;右表T2中不符合條件,就不用加入結果表中,并且NULL? ? ? ? ? ? ? 表示。
? SQL語句:select * from T1 left outer join T2 on T1.userid=T2.userid
| 運行結果 | T1.userid | username | password | T2.userid | jifen | dengji |
| ? | 1 | jack | jackpwd | 1 | 20 | 3 |
| ? | 2 | owen | owenpwd | NULL | NULL | NULL |
右聯:顯示右表T2中的所有行,并把左表T1中符合條件加到右表T2中;左表T1中不符合條件,就不用加入結果表中,并且NULL? ? ? ? ? ? ? 表示。
? ? ? ? ??SQL語句:select * from T1 right outer join T2 on T1.userid=T2.userid
| 運行結果 | T1.userid | username | password | T2.userid | jifen | dengji |
| ? | 1 | jack | jackpwd | 1 | 20 | 3 |
| ? | NULL | NULL | NULL | 3 | 50 | 6 |
全聯:顯示左表T1、右表T2兩邊中的所有行,即把左聯結果表+右聯結果表組合在一起,然后過濾掉重復的。
? ? SQL語句:select * from T1 full outer join T2 on T1.userid=T2.userid
| 運行結果 | T1.userid | username | password | T2.userid | jifen | dengji |
| ? | 1 | jack | jackpwd | 1 | 20 | 3 |
| ? | 2 | owen | owenpwd | NULL | NULL | NULL |
| ? | NULL | NULL | NULL | 3 | 50 | 6 |
8. 更新語句
update information set name="kk" where name="wk";9. 刪除語句
mysql> delete from information where name="kk";10. 刪除數據庫
drop database student;11. 刪除表
mysql> drop tables information; Query OK, 0 rows affected (0.00 sec)?
總結
以上是生活随笔為你收集整理的MySql 复习SQL基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Airsim仿真
- 下一篇: ROS修改pkg名和node名教程