国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

MySQL插入 更新與刪掉數(shù)據(jù)

2024-07-24 12:35:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
   存儲(chǔ)在系統(tǒng)中的數(shù)據(jù)是數(shù)據(jù)庫(kù)管理系統(tǒng)(DBMS)的核心,數(shù)據(jù)庫(kù)被設(shè)計(jì)用來(lái)管理數(shù)據(jù)的存儲(chǔ)、訪(fǎng)問(wèn)和維護(hù)數(shù)據(jù)的完整性。MySQL中提供豐富的數(shù)據(jù)管理語(yǔ)句,包括插入數(shù)據(jù)的INSERT、更新數(shù)據(jù)的UPDATE以及刪除數(shù)據(jù)的DELETE語(yǔ)句。
 
  1、插入數(shù)據(jù)
 
  為表的所有字段插入數(shù)據(jù)
 
   使用基本的INSERT語(yǔ)句插入數(shù)據(jù),要求指定表名稱(chēng)和插入到新紀(jì)錄中的值,其基本語(yǔ)法為:
 
  INSERT INTO tbl_name (column_list) VALUES (value_list)
  在插入數(shù)據(jù)前,首先創(chuàng)建一張表:
 
  mysql> CREATE TABLE person
      -> (
      -> id     INT UNSIGNED NOT NULL AUTO_INCREMENT,
      -> name   CHAR(40) NOT NULL DEFAULT '',
      -> age    INT NOT NULL DEFAULT 0,
      -> info   CHAR(50) NULL,
      -> PRIMARY KEY (id)
      -> );
  Query OK, 0 rows affected (0.03 sec)
  在person表中,插入一條新記錄,id值為1,name值為Green,age值為21,info值為L(zhǎng)awyer
 
  mysql>  INSERT INTO person (id ,name, age , info)
      ->       VALUES (1,'Green', 21, 'Lawyer');
  Query OK, 1 row affected (0.02 sec)
 
  mysql> SELECT * FROM person;
  +----+-------+-----+--------+
  | id | name  | age | info   |
  +----+-------+-----+--------+
  |  1 | Green |  21 | Lawyer |
  +----+-------+-----+--------+
  1 row in set (0.00 sec)
  在person表中,插入一條新記錄,id值為2,name值為Suse,age值為22,info值為dancer
 
  mysql>  INSERT INTO person (age ,name, id , info)
      ->       VALUES (22, 'Suse', 2, 'dancer');
  Query OK, 1 row affected (0.02 sec)
 
  mysql> SELECT * FROM person;
  +----+-------+-----+--------+
  | id | name  | age | info   |
  +----+-------+-----+--------+
  |  1 | Green |  21 | Lawyer |
  |  2 | Suse  |  22 | dancer |
  +----+-------+-----+--------+
  2 rows in set (0.00 sec)
  為表的指定字段插入數(shù)據(jù)
 
  在person表中,插入一條新記錄,name值為Willam,age值為20,info值為sports man
 
  mysql>  INSERT INTO person (name, age,info)
      ->      VALUES('Willam', 20, 'sports man');
  Query OK, 1 row affected (0.02 sec)
 
  mysql> SELECT * FROM person;                                                       
  +----+--------+-----+------------+
  | id | name   | age | info       |
  +----+--------+-----+------------+
  |  1 | Green  |  21 | Lawyer     |
  |  2 | Suse   |  22 | dancer     |
  |  3 | Willam |  20 | sports man |
  +----+--------+-----+------------+
  3 rows in set (0.00 sec)
  在person表中,插入一條新記錄,name值為laura,age值為25
 
  mysql>  INSERT INTO person (name, age ) VALUES ('Laura', 25);
  Query OK, 1 row affected (0.01 sec)
 
  mysql> SELECT * FROM person;                                 
  +----+--------+-----+------------+
  | id | name   | age | info       |
  +----+--------+-----+------------+
  |  1 | Green  |  21 | Lawyer     |
  |  2 | Suse   |  22 | dancer     |
  |  3 | Willam |  20 | sports man |
  |  4 | Laura  |  25 | NULL       |
  +----+--------+-----+------------+
  4 rows in set (0.00 sec)
    可以發(fā)現(xiàn)id字段在插入數(shù)據(jù)后,沒(méi)有賦值時(shí)自動(dòng)增加,在這里id字段為表的主鍵,不能為空,紫銅會(huì)自動(dòng)為字段插入自增的序列值。
 
  同時(shí)插入多條記錄
 
    INSERT語(yǔ)句可以同時(shí)相數(shù)據(jù)表中插入多條記錄,插入時(shí)指定多個(gè)值列表,每個(gè)值列表之間用逗號(hào)分隔開(kāi),基本語(yǔ)法為:
 
  INSERT INTO tbl_name (column_list)
    VALUES (value_list1), (value_list2),(value_list3);
  在person表中,在name、age和info字段指定插入值,同時(shí)插入3條新記錄
 
  mysql>  INSERT INTO person(name, age, info)
      ->      VALUES ('Evans',27, 'secretary'),
      ->      ('Dale',22, 'cook'),
      ->      ('Edison',28, 'singer');
  Query OK, 3 rows affected (0.02 sec)
  Records: 3  Duplicates: 0  Warnings: 0
 
  mysql> SELECT * FROM person;                                                              
  +----+--------+-----+------------+
  | id | name   | age | info       |
  +----+--------+-----+------------+
  |  1 | Green  |  21 | Lawyer     |
  |  2 | Suse   |  22 | dancer     |
  |  3 | Willam |  20 | sports man |
  |  4 | Laura  |  25 | NULL       |
  |  5 | Evans  |  27 | secretary  |
  |  6 | Dale   |  22 | cook       |
  |  7 | Edison |  28 | singer     |
  +----+--------+-----+------------+
  7 rows in set (0.00 sec)
  在person表中,不指定插入列表,同時(shí)插入2條新記錄
 
  mysql>  INSERT INTO person
      ->      VALUES (9,'Harry',21, 'magician'),
      ->      (NULL,'Harriet',19, 'pianist');
  Query OK, 2 rows affected (0.02 sec)
  Records: 2  Duplicates: 0  Warnings: 0
 
  mysql> SELECT * FROM person;                                                              
  +----+---------+-----+------------+
  | id | name    | age | info       |
  +----+---------+-----+------------+
  |  1 | Green   |  21 | Lawyer     |
  |  2 | Suse    |  22 | dancer     |
  |  3 | Willam  |  20 | sports man |
  |  4 | Laura   |  25 | NULL       |
  |  5 | Evans   |  27 | secretary  |
  |  6 | Dale    |  22 | cook       |
  |  7 | Edison  |  28 | singer     |
  |  9 | Harry   |  21 | magician   |
  | 10 | Harriet |  19 | pianist    |
  +----+---------+-----+------------+
  9 rows in set (0.00 sec)
  將查詢(xún)結(jié)果插入數(shù)據(jù)
 
   INSERT語(yǔ)句用來(lái)給數(shù)據(jù)表插入記錄時(shí),指定插入記錄的列值。INSERT還可以將SELECT語(yǔ)句查詢(xún)的結(jié)果插入到列表中,其基本語(yǔ)法為:
 
  INSERT INTO tbl_name1 (column_list1)
    SELECT (column_list2) FROM table_name2 WHERE (condition)
  從person_old表中查詢(xún)所有的記錄,并將其插入到person表中
 
  首先,創(chuàng)建一個(gè)名為person_old的數(shù)據(jù)表,其表結(jié)構(gòu)與person結(jié)構(gòu)相同
  mysql> CREATE TABLE person_old
      -> (
      -> id     INT UNSIGNED NOT NULL AUTO_INCREMENT,
      -> name   CHAR(40) NOT NULL DEFAULT '',
      -> age    INT NOT NULL DEFAULT 0,
      -> info   CHAR(50) NULL,
      -> PRIMARY KEY (id)
      -> );
  Query OK, 0 rows affected (0.11 sec)
 
  向person_old表中添加兩條記錄
  mysql>  INSERT INTO person_old
      ->      VALUES (11,'Harry',20, 'student'), (12,'Beckham',31, 'police');
  Query OK, 2 rows affected (0.20 sec)
  Records: 2  Duplicates: 0  Warnings: 0
 
  mysql> SELECT * FROM person_old;
  +----+---------+-----+---------+
  | id | name    | age | info    |
  +----+---------+-----+---------+
  | 11 | Harry   |  20 | student |
  | 12 | Beckham |  31 | police  |
  +----+---------+-----+---------+
  2 rows in set (0.00 sec)
 
  插入數(shù)據(jù)到person表中
  mysql> INSERT INTO person(id, name, age, info)
      ->      SELECT id, name, age, info FROM person_old;
  Query OK, 2 rows affected (0.01 sec)
  Records: 2  Duplicates: 0  Warnings: 0
 
  mysql> SELECT * FROM person;
  +----+---------+-----+------------+
  | id | name    | age | info       |
  +----+---------+-----+------------+
  |  1 | Green   |  21 | Lawyer     |
  |  2 | Suse    |  22 | dancer     |
  |  3 | Willam  |  20 | sports man |
  |  4 | Laura   |  25 | NULL       |
  |  5 | Evans   |  27 | secretary  |
  |  6 | Dale    |  22 | cook       |
  |  7 | Edison  |  28 | singer     |
  |  9 | Harry   |  21 | magician   |
  | 10 | Harriet |  19 | pianist    |
  | 11 | Harry   |  20 | student    |
  | 12 | Beckham |  31 | police     |
  +----+---------+-----+------------+
  11 rows in set (0.00 sec)
  2、更新數(shù)據(jù)
 
    表中有數(shù)據(jù)之后,可以對(duì)數(shù)據(jù)進(jìn)行更新,其基本語(yǔ)法為:
 
  UPDATE table_name
      SET col_name1=value1,col_name2=value2,...,
      WHERE where_condition
  在person表中,更新id值為11的記錄,將age字段值改為15,將name字段值改為L(zhǎng)iMing
 
  mysql> UPDATE person SET age = 15, name='LiMing' WHERE id = 11;
  Query OK, 1 row affected (0.02 sec)
  Rows matched: 1  Changed: 1  Warnings: 0
 
  mysql> SELECT * FROM person WHERE id=11;
  +----+--------+-----+---------+
  | id | name   | age | info    |
  +----+--------+-----+---------+
  | 11 | LiMing |  15 | student |
  +----+--------+-----+---------+
  1 row in set (0.00 sec)
  在person表中,更新age值為19~22的記錄,將info字段值都改為student
 
  mysql> UPDATE person SET info='student'  WHERE age  BETWEEN 19 AND 22;  
  Query OK, 6 rows affected (0.02 sec)
  Rows matched: 6  Changed: 6  Warnings: 0
 
  mysql> SELECT * FROM person WHERE age BETWEEN 19 AND 22;                 
  +----+---------+-----+---------+
  | id | name    | age | info    |
  +----+---------+-----+---------+
  |  1 | Green   |  21 | student |
  |  2 | Suse    |  22 | student |
  |  3 | Willam  |  20 | student |
  |  6 | Dale    |  22 | student |
  |  9 | Harry   |  21 | student |
  | 10 | Harriet |  19 | student |
  +----+---------+-----+---------+
  6 rows in set (0.00 sec)
  3、刪除數(shù)據(jù)
 
    從數(shù)據(jù)表中刪除數(shù)據(jù)使用DELETE語(yǔ)句,其基本語(yǔ)法為:
 
  DELETE FROMN table_name [WHERE <condition>]
  在person表中,刪除id等于11的記錄
 
  執(zhí)行刪除操作前,使用SELECT語(yǔ)句查看當(dāng)前id=11的記錄
  mysql>  SELECT * FROM person WHERE id=11;
  +----+--------+-----+---------+
  | id | name   | age | info    |
  +----+--------+-----+---------+
  | 11 | LiMing |  15 | student |
  +----+--------+-----+---------+
  1 row in set (0.00 sec)
 
  使用DELETE語(yǔ)句刪除該記錄
  mysql>  DELETE FROM person WHERE id = 11;
  Query OK, 1 row affected (0.02 sec)
 
  語(yǔ)句執(zhí)行完畢,查看執(zhí)行結(jié)果:
  mysql>  SELECT * FROM person WHERE id=11;
  Empty set (0.00 sec)
  在person表中,使用DELETE語(yǔ)句同時(shí)刪除多條記錄,刪除age字段在19-22的記錄
 
  執(zhí)行刪除操作前,使用SELECT語(yǔ)句查看當(dāng)前的數(shù)據(jù)
  mysql>  SELECT * FROM person WHERE age BETWEEN 19 AND 22;
  +----+---------+-----+---------+
  | id | name    | age | info    |
  +----+---------+-----+---------+
  |  1 | Green   |  21 | student |
  |  2 | Suse    |  22 | student |
  |  3 | Willam  |  20 | student |
  |  6 | Dale    |  22 | student |
  |  9 | Harry   |  21 | student |
  | 10 | Harriet |  19 | student |
  +----+---------+-----+---------+
  6 rows in set (0.00 sec)
 
  DELETE刪除這些記錄
  mysql>  DELETE FROM person WHERE age BETWEEN 19 AND 22;
  Query OK, 6 rows affected (0.01 sec)
 
  查看執(zhí)行結(jié)果
  mysql>  SELECT * FROM person WHERE age BETWEEN 19 AND 22;
  Empty set (0.00 sec)
  刪除person表中所有記錄,SQL語(yǔ)句如下
 
  執(zhí)行刪除操作前,使用SELECT語(yǔ)句查看當(dāng)前的數(shù)據(jù):
   mysql> SELECT * FROM person;
  +----+---------+-----+-----------+
  | id | name    | age | info      |
  +----+---------+-----+-----------+
  |  4 | Laura   |  25 | NULL      |
  |  5 | Evans   |  27 | secretary |
  |  7 | Edison  |  28 | singer    |
  | 12 | Beckham |  31 | police    |
  +----+---------+-----+-----------+
  4 rows in set (0.00 sec)
 
  執(zhí)行DELETE語(yǔ)句刪除這4條記錄
  mysql>  DELETE FROM person;
  Query OK, 4 rows affected (0.01 sec)
 
  查看執(zhí)行結(jié)果:
  mysql>  SELECT * FROM person;
  Empty set (0.00 sec)
    如果想刪除表中的所有記錄,還可以使用TRUNCATE TABLE語(yǔ)句,TRUNCATE將直接刪除原來(lái)的表并重新創(chuàng)建一個(gè)表,其語(yǔ)法結(jié)構(gòu)為T(mén)RUNCATE TABLE table_name。TRUNCATE直接刪除表而不是刪除記錄,因此執(zhí)行速度比DELETE快。

(編輯:武林網(wǎng))

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 固原市| 无棣县| 星座| 泽州县| 邢台县| 巩留县| 南平市| 屯昌县| 宜兰市| 靖西县| 怀远县| 瓮安县| 福海县| 乐亭县| 进贤县| 十堰市| 齐河县| 丰顺县| 嫩江县| 兴业县| 宜良县| 前郭尔| 开原市| 灵璧县| 普陀区| 峨眉山市| 台州市| 宝清县| 类乌齐县| 普格县| 扶沟县| 葫芦岛市| 吉木萨尔县| 高尔夫| 凤庆县| 蚌埠市| 上饶县| 阿克陶县| 大化| 芦山县| 佛冈县|