alter table info add primary key(id); Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc info;..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql數(shù)據(jù)庫基本命令---多條數(shù)據(jù)的同時(shí)操作

發(fā)布時(shí)間:2020-07-31 18:36:08 來源:網(wǎng)絡(luò) 閱讀:989 作者:低調(diào)的男孩 欄目:MySQL數(shù)據(jù)庫

添加主鍵
mysql> alter table info add primary key(id);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc info;  #查看主鍵
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id     | int(11)      | NO   | PRI | NULL    |       |    #顯示出主鍵
| name   | char(6)      | YES  |     | NULL    |       |
| score  | decimal(5,2) | YES  |     | NULL    |       |
| age    | int(4)       | YES  |     | NULL    |       |
| hobbly | int(4)       | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

添加外鍵
alter table info add constraint FK_ID foreign key(hobbly) REFERENCES hy(id);

mysql> show index from info\G;    #查看外鍵
* 2. row *
        Table: info
   Non_unique: 1
     Key_name: FK_ID     #外鍵添加成功
 Seq_in_index: 1
  Column_name: hobbly
    Collation: A
  Cardinality: 3
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

表格同時(shí)修改個(gè)字段(不同表列內(nèi)容)
update info set age=23,num=44 where id=2;   
mysql> select * from info;
+----+--------+-------+-----------+------+------+
| id | name   | score | hobbly    | age  | num  |
+----+--------+-------+-----------+------+------+
|  1 | zl     | 88    | 看書      |   33 | NULL |
|  2 | 李四   | 68    | 上網(wǎng)      |   23 |   44 |        #成功修改
|  3 | 王四   | 68    | 看電視    | NULL | NULL |
+----+--------+-------+-----------+------+------+
3 rows in set (0.00 sec)

同時(shí)增加多個(gè)字段
mysql> alter table info add column age int,add column num int;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc info; #查看表結(jié)構(gòu)
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id     | int(11)  | NO   | PRI | NULL    |       |
| name   | char(4)  | YES  |     | NULL    |       |
| score  | char(6)  | YES  |     | NULL    |       |
| hobbly | char(10) | YES  |     | NULL    |       |
| age    | int(11)  | YES  |     | NULL    |       |
| num    | int(11)  | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+

同時(shí)刪除多個(gè)字段
mysql> alter table info drop column age,drop column num;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from info;
+----+--------+-------+-----------+
| id | name   | score | hobbly    |
+----+--------+-------+-----------+
|  1 | zl     | 88    | 看書      |
|  2 | 李四   | 68    | 上網(wǎng)      |
|  3 | 王四   | 68    | 看電視    |
+----+--------+-------+-----------+

同時(shí)刪除多個(gè)記錄條數(shù)(多行)
mysql> delete from info where id in (3,4);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from info;
+----+--------+-------+--------+
| id | name   | score | hobbly |
+----+--------+-------+--------+
|  1 | zl     | 88    | 看書   |
|  2 | 李四   | 68    | 上網(wǎng)   |
|  5 | kl     | 88    | 游泳   |
+----+--------+-------+--------+

同時(shí)查看多條指定記錄(行)
mysql> select * from info where id in (3,4);
+----+--------+-------+-----------+
| id | name   | score | hobbly    |
+----+--------+-------+-----------+
|  3 | 王四   | 68    | 看電視    |
|  4 | ll         | 67    | 看書      |
+----+--------+-------+-----------+
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI