1. 程式人生 > >mysql 常見語句

mysql 常見語句

建立資料庫

create database practice;

顯示資料庫

show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| dearabao           |
| hello              |
| link_draw          |
| link_group         |
| link_user          |
| mysql              |
| performance_schema | | person | | practice | | tempdb | | test | +--------------------+

使用資料庫

use practice;

建立資料表

create table `practice_2`(
    `id` int(11) unsigned not null auto_increment comment '主鍵id',
    `name` varchar(16) NOT NULL COMMENT '姓名'
, `age` tinyint(4) NOT NULL COMMENT '年齡', `CID` bigint(20) NOT NULL COMMENT '身份證ID', PRIMARY KEY(`id`), UNIQUE KEY `uk_cid` (`CID`) ) ENGINE=InnoDb DEFAULT CHARSET=utf8 COMMENT='測試表';

顯示資料表

show tables;

+--------------------+
| Tables_in_practice |
+--------------------+
| practice_1         |
+--------------------+

顯示資料表結構

show create table practice_1;

+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                                                                                                                                                                                                  |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| practice_1 | CREATE TABLE `practice_1` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
  `name` varchar(16) NOT NULL COMMENT '姓名',
  `age` tinyint(4) NOT NULL COMMENT '年齡',
  `CID` bigint(20) NOT NULL COMMENT '身份證ID',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_cid` (`CID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='測試表'             |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

插入資料

  • 插入單條資料
insert practice_1 (`name`, `age`, `CID`) values ('jack', 20, 20072222);
  • 插入多條資料
mysql> insert practice_1 (`name`, `age`, `CID`) values ('jack', 20, 20072222), ('jhon', 21, 20072003) on duplicate key update `name` = values(name), `age` = values(age), `CID` = values(CID);
Query OK, 1 row affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
  • 插入命中唯一索引的資料
mysql> insert practice_1 (`name`, `age`, `CID`) values ('jack', 20, 20072222);
ERROR 1062 (23000): Duplicate entry '20072222' for key 'uk_cid'

mysql> insert practice_1 (`name`, `age`, `CID`) values ('jack', 20, 20072222) on duplicate key update `name` = values(name), `age` = values(age), `CID` = values(CID);
Query OK, 0 rows affected (0.00 sec)

刪除資料

mysql> delete from practice_1 where CID = 20072103;
Query OK, 1 row affected (0.01 sec)

刪除表資料

## 建立practice_2表
mysql> create table `practice_2`(
    -> `id` int(11) unsigned not null auto_increment comment '主鍵id',
    ->     `name` varchar(16) NOT NULL COMMENT '姓名',
    ->     `age` tinyint(4) NOT NULL COMMENT '年齡',
    ->     `CID` bigint(20) NOT NULL COMMENT '身份證ID',  
    ->     PRIMARY KEY(`id`), 
    ->     UNIQUE KEY `uk_cid` (`CID`)
    -> ) ENGINE=InnoDb DEFAULT CHARSET=utf8 COMMENT='測試表';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

## 
mysql> show tables;
+--------------------+
| Tables_in_practice |
+--------------------+
| practice_1         |
| practice_2         |
+--------------------+
2 rows in set (0.00 sec)

## 複製practice_1表資料到practice_2
mysql> insert into practice_2 select * from practice_1;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from practice_2;
+----+------+-----+----------+
| id | name | age | CID      |
+----+------+-----+----------+
|  1 | jack |  20 | 20072222 |
|  2 | jhon |  21 | 20072003 |
|  3 | Jim  |  20 | 20072253 |
+----+------+-----+----------+
3 rows in set (0.00 sec)

mysql> truncate practice_2; // 表資料被刪, 表結構儲存

刪除表

mysql> drop table practice_2;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> 
mysql> show tables;
+--------------------+
| Tables_in_practice |
+--------------------+
| practice_1         |
+--------------------+
1 row in set (0.00 sec)

刪除資料庫

mysql> drop database practise;
Query OK, 0 rows affected (0.01 sec)

聯表去重查詢

mysql> select distinct(`name`) from (select * from practice_1 union all select * from practice_2) as t where 1 = 1;
+-------+
| name  |
+-------+
| jack  |
| jhon  |
| Jim   |
| henry |
+-------+