MYSQL 8 安装之04(表基本操作)

  1. 創建表
[email protected] : testdb【07:29:19】106 SQL->
create table if not exists cust
 (
  id int unsigned auto_increment primary key,
  first_name varchar (20),
  last_name varchar (20) ,
  country varchar (20)
 ) ENGINE=InnoDB;

#AUTO I N CREMENT : 自动生成线性递增序列

[email protected] : testdb【07:29:22】107 SQL->show create table cust \G;
*************************** 1. row ***************************
       Table: cust
Create Table: CREATE TABLE `cust` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) DEFAULT NULL,
  `last_name` varchar(20) DEFAULT NULL,
  `country` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

[email protected] : testdb【07:33:19】108 SQL->desc cust;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| first_name | varchar(20)      | YES  |     | NULL    |                |
| last_name  | varchar(20)      | YES  |     | NULL    |                |
| country    | varchar(20)      | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

[email protected] : testdb【07:42:30】123 SQL->
insert into cust(first_name,last_name,country) values('Mike','Christensen','USA');

[email protected] : testdb【07:46:00】124 SQL->select * from cust;
+----+------------+-------------+---------+
| id | first_name | last_name   | country |
+----+------------+-------------+---------+
|  1 | Mike       | Christensen | USA     |
+----+------------+-------------+---------+

[email protected] : testdb【12:48:17】25 SQL->
insert into cust(first_name,last_name,country) values('Mike2','Christensen','USA'),('Andy','Hollands','Australia');

[email protected] : testdb【12:48:22】26 SQL->select * from cust;                                                                               
+----+------------+-------------+-----------+
| id | first_name | last_name   | country   |
+----+------------+-------------+-----------+
|  1 | Mike       | Christensen | USA       |
|  2 | Mike2      | Christensen | USA       |
|  3 | Andy       | Hollands    | Australia |
+----+------------+-------------+-----------+

#insert ignore 忽略已經存在的數據(同時警告)
[email protected] : testdb【12:48:27】27 SQL->
insert ignore into cust(id,first_name,last_name,country) values(1,'Mike2','Chris','USA'),(4,'Andy','Holla','Aus');
Query OK, 1 row affected, 1 warning (0.35 sec)
Records: 2  Duplicates: 1  Warnings: 1

[email protected] : testdb【12:58:26】28 SQL->select * from cust;                                                                               
+----+------------+-------------+-----------+
| id | first_name | last_name   | country   |
+----+------------+-------------+-----------+
|  1 | Mike       | Christensen | USA       |
|  2 | Mike2      | Christensen | USA       |
|  3 | Andy       | Hollands    | Australia |
|  4 | Andy       | Holla       | Aus       |
+----+------------+-------------+-----------+
#replace 刪除已經存在的數據,並插入新數據,如不存在則插入。
[email protected] : testdb【01:06:02】33 SQL->
replace into cust(id,first_name,last_name,country) values(1,'Mike2','Chris','USA'),(5,'Andy','Holla','Aus');
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

[email protected] : testdb【01:07:00】34 SQL->select * from cust;                                                                               
+----+------------+-----------+-----------+
| id | first_name | last_name | country   |
+----+------------+-----------+-----------+
|  1 | Mike2      | Chris     | USA       |
|  2 | Andy       | Holla     | Aus       |
|  3 | Andy       | Hollands  | Australia |
|  4 | Andy       | Holla     | Aus       |
|  5 | Andy       | Holla     | Aus       |
+----+------------+-----------+-----------+

#如果你想在行已经存在的情况下处理重复项, 则需要使用ON DUPLICATE KEYUPDATE 。
#如果指定了ON DUPLICATE KEY UPDATE 选项,并且INSERT 语句在PRIMARY KEY 中引发了重复值, 
#则MySQL 会用新值更新已有行
[email protected] : testdb【01:15:02】50 SQL->
create table if not exists pays
 (
  id int unsigned auto_increment primary key,
  user_name varchar (20),
  payment  decimal(10,2)
 ) ENGINE=InnoDB;

[email protected] : testdb【01:15:16】51 SQL->
insert into pays(user_name,payment) values('xag',100.00),('yyc',200.00);

[email protected] : testdb【01:16:00】52 SQL->select * from pays;
+----+-----------+---------+
| id | user_name | payment |
+----+-----------+---------+
|  1 | xag       |  100.00 |
|  2 | yyc       |  200.00 |
+----+-----------+---------+

[email protected] : testdb【01:16:06】53 SQL->
insert into pays(id,user_name,payment) values(1,'xag',500.00) on duplicate key update payment=payment+values(payment);
Query OK, 2 rows affected (0.06 sec)

[email protected] : testdb【01:18:05】54 SQL->select * from pays;                                                                               
+----+-----------+---------+
| id | user_name | payment |
+----+-----------+---------+
|  1 | xag       |  600.00 |
|  2 | yyc       |  200.00 |
+----+-----------+---------+

[email protected] : testdb【01:18:08】55 SQL->insert into pays(id,user_name,payment) values(3,'xrj',500.00) on duplicate key update payment=paymeent+values(payment);
Query OK, 1 row affected (0.06 sec)

[email protected] : testdb【01:18:44】56 SQL->select * from pays;                                                                               
+----+-----------+---------+
| id | user_name | payment |
+----+-----------+---------+
|  1 | xag       |  600.00 |
|  2 | yyc       |  200.00 |
|  3 | xrj       |  500.00 |
+----+-----------+---------+

你可能感兴趣的:(MYSQL 8 安装之04(表基本操作))