我的Linux生涯之Mysql:Day02[Mysql的基本管理]練習

練習:
1、在yg_tab表裏添加名id的字段,在所有字段的上方,爲主鍵字段且有自動增長功能。把表中的name和sex字段設置爲index字段。
在部門字段下方添加mail字段默認值是
[email protected]
在id字段下添加名爲yg_id字段,字段值不允許重複,
在mail字段下方添加loves字段保存員工的愛好 默認愛好 IT,film
2、查看錶結構看設置是否正確
3、向表中插入3條新的員式記錄
4、查看記錄是否插入成功
5、刪除表裏所有字段的索引屬性
6、查看錶結構是否刪除成功。
7、複製當前表新表名爲 newuser

8、刪除yg_tab表中所有記錄

 


mysql> alter table yg_tab modify id int(2) not null primary key auto_increment;
///在yg_tab表中添id字段在最上方,字段類型int(2)
//不允許空值not null
//設置爲主鍵primary key 
//自動增長功能 auto_increment
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0 

 

mysql> select * from yg_tab;
//查看yg_tab表的內容。
+----+------+--------+------+-----------+----------+------+------------+---------+
| id | name | gender | age  | entrytime | position | work | department | work_id |
+----+------+--------+------+-----------+----------+------+------------+---------+
|  1 | tom  | boy    |   23 |      2013 | staff    | IT   | plan       |     100 | 
|  2 | jea  | gril   |   18 |      2012 | manager  | chef | finance    |     100 | 
|  3 | kek  | boy    |   24 |      2010 | staff    | driv | personnel  |     100 | 
+----+------+--------+------+-----------+----------+------+------------+---------+
3 rows in set (0.00 sec)

 

 
mysql> create index name on yg_tab(name);
//爲yg_tab表的name字段設置index
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
//設置name字段爲索引,索引名爲name  

 

 

 
mysql> create index gender on yg_tab(name);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
//同上。  

 

 
mysql> alter table yg_tab add loves set("IT","film","game") default "IT,film";
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 
//添加字段loves,
//多選 set("IT","film","game") 
//默認值爲"IT,film"  

 

 
mysql> alter table yg_tab modify  mail char(15) not null ;
//添加yg_tab表中的mail字段
//指定寬度爲15字節。
//不允許爲空值 not null
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0.  

 

mysql>insert into yg_tab values ("6","anmi","gril","24","2013","manager","chef","finance","106","[email protected]","film");
//添加記錄。

 

 
mysql> select * from yg_tab;
+----+------+--------+-----+-----------+----------+------+------------+---------+-----------------+---------+
| id | name | gender | age | entrytime | position | work | department | work_id | mail            | loves   |
+----+------+--------+-----+-----------+----------+------+------------+---------+-----------------+---------+
|  1 | tom  | boy    |  23 |      2013 | staff    | IT   | plan       |     100 | [email protected] | IT,film | 
|  2 | jea  | gril   |  18 |      2012 | manager  | chef | finance    |     100 | [email protected] | IT,film | 
|  3 | kek  | boy    |  24 |      2010 | staff    | driv | personnel  |     100 | [email protected] | IT,film | 
|  4 | heh  | boy    |  18 |      2011 | staff    | IT   | plan       |     101 | [email protected]  | IT      | 
|  5 | lia  | boy    |  34 |      2003 | manager  | chef | finance    |     103 | [email protected] | film    | 
|  6 | anm  | gril   |  24 |      2013 | manager  | chef | finance    |     106 | [email protected] | film    | 
+----+------+--------+-----+-----------+----------+------+------------+---------+-----------------+---------+
6 rows in set (0.00 sec)  

 

 
mysql> create table newuser select * from ygdb.yg_tab;
//複製ygdb.yg_tab,新表名爲newuser
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0  

 

 

mysql> show tables;
+----------------+
| Tables_in_ygdb |
+----------------+
| newuser        | 
| yg_tab         | 
+----------------+
2 rows in set (0.00 sec

 

 
mysql> alter table yg_tab modify id int(2) not null;
//yg_tab表中的id字段保留類型,修改不能爲空值
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0  

 

 
mysql> alter table yg_tab drop primary key;       
//刪除yg_tab表的主鍵  
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0  

 

 
mysql> delete from yg_tab;
//刪除yg_tab表的所有內容
Query OK, 6 rows affected (0.00 sec)  

 

 

 

 

 

 

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