MySQL入门
Posted youngchaolin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL入门相关的知识,希望对你有一定的参考价值。
这里简单的记录一下,mysql的基本语法,包括建表,删除表,以及增删改查等操作。
数据库的创建
建表常用使用语法为CREATE DATABASE [IF NOT EXISTS] 表名 ,后面也可以指定字符集和校对规则,如果不指定就是默认的,可以通过命令来查看。
1 CREATE DATABASE mydb1; 2 --指定字符集 3 CREATE DATABASE mydb2 CHARACTER SET gbk; 4 --查看mysql存储位置 5 SHOW GLOBAL VARIABLES LIKE "%datadir%"; 6 --添加校对规则,可以查看mysql文档 7 CREATE DATABASE mydb3 CHARACTER SET utf8 COLLATE utf8_bin; 8 --创建一个mydb1,使用关键字 9 CREATE DATABASE IF NOT EXISTS mydb1;
可以查看数据库存储的位置
通过value值提示,去到C盘去寻找,可以看出来刚才创建的数据库存放的实际位置。
点进去有一个db.opt文件,可以查看数据库里的的基本设置,可以看出默认字符集为utf-8,默认校对规则是utf8_general_ci,一个字符集可以有很多校对规则,默认为ci结尾的,可以通过命令或者查看文档来查看。
如下通过命令查看了数据库mydb2的字符集合和校对规则,这个得到的结果和上面一样。
1 mysql> use mydb2; 2 Database changed 3 mysql> show character set; 4 +----------+-----------------------------+---------------------+--------+ 5 | Charset | Description | Default collation | Maxlen | 6 +----------+-----------------------------+---------------------+--------+ 7 | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | 8 | dec8 | DEC West European | dec8_swedish_ci | 1 | 9 | cp850 | DOS West European | cp850_general_ci | 1 | 10 | hp8 | HP West European | hp8_english_ci | 1 | 11 | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | 12 | latin1 | cp1252 West European | latin1_swedish_ci | 1 | 13 | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | 14 | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | 15 | ascii | US ASCII | ascii_general_ci | 1 | 16 | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | 17 | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | 18 | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | 19 | tis620 | TIS620 Thai | tis620_thai_ci | 1 | 20 | euckr | EUC-KR Korean | euckr_korean_ci | 2 | 21 | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | 22 | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | 23 | greek | ISO 8859-7 Greek | greek_general_ci | 1 | 24 | cp1250 | Windows Central European | cp1250_general_ci | 1 | 25 | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | 26 | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | 27 | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | 28 | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | 29 | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | 30 | cp866 | DOS Russian | cp866_general_ci | 1 | 31 | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | 32 | macce | Mac Central European | macce_general_ci | 1 | 33 | macroman | Mac West European | macroman_general_ci | 1 | 34 | cp852 | DOS Central European | cp852_general_ci | 1 | 35 | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | 36 | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | 37 | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | 38 | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | 39 | cp1256 | Windows Arabic | cp1256_general_ci | 1 | 40 | cp1257 | Windows Baltic | cp1257_general_ci | 1 | 41 | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | 42 | binary | Binary pseudo charset | binary | 1 | 43 | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | 44 | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | 45 | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | 46 +----------+-----------------------------+---------------------+--------+ 47 39 rows in set (0.00 sec) 48 49 mysql> show collation like ‘%utf8%‘; 50 +--------------------------+---------+-----+---------+----------+---------+ 51 | Collation | Charset | Id | Default | Compiled | Sortlen | 52 +--------------------------+---------+-----+---------+----------+---------+ 53 | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | 54 | utf8_bin | utf8 | 83 | | Yes | 1 | 55 | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | 56 | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | 57 | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | 58 | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | 59 | utf8_slovenian_ci | utf8 | 196 | | Yes | 8 | 60 | utf8_polish_ci | utf8 | 197 | | Yes | 8 | 61 | utf8_estonian_ci | utf8 | 198 | | Yes | 8 | 62 | utf8_spanish_ci | utf8 | 199 | | Yes | 8 | 63 | utf8_swedish_ci | utf8 | 200 | | Yes | 8 | 64 | utf8_turkish_ci | utf8 | 201 | | Yes | 8 | 65 | utf8_czech_ci | utf8 | 202 | | Yes | 8 | 66 | utf8_danish_ci | utf8 | 203 | | Yes | 8 | 67 | utf8_lithuanian_ci | utf8 | 204 | | Yes | 8 | 68 | utf8_slovak_ci | utf8 | 205 | | Yes | 8 | 69 | utf8_spanish2_ci | utf8 | 206 | | Yes | 8 | 70 | utf8_roman_ci | utf8 | 207 | | Yes | 8 | 71 | utf8_persian_ci | utf8 | 208 | | Yes | 8 | 72 | utf8_esperanto_ci | utf8 | 209 | | Yes | 8 | 73 | utf8_hungarian_ci | utf8 | 210 | | Yes | 8 | 74 | utf8_sinhala_ci | utf8 | 211 | | Yes | 8 | 75 | utf8_general_mysql500_ci | utf8 | 223 | | Yes | 1 | 76 | utf8mb4_general_ci | utf8mb4 | 45 | Yes | Yes | 1 | 77 | utf8mb4_bin | utf8mb4 | 46 | | Yes | 1 | 78 | utf8mb4_unicode_ci | utf8mb4 | 224 | | Yes | 8 | 79 | utf8mb4_icelandic_ci | utf8mb4 | 225 | | Yes | 8 | 80 | utf8mb4_latvian_ci | utf8mb4 | 226 | | Yes | 8 | 81 | utf8mb4_romanian_ci | utf8mb4 | 227 | | Yes | 8 | 82 | utf8mb4_slovenian_ci | utf8mb4 | 228 | | Yes | 8 | 83 | utf8mb4_polish_ci | utf8mb4 | 229 | | Yes | 8 | 84 | utf8mb4_estonian_ci | utf8mb4 | 230 | | Yes | 8 | 85 | utf8mb4_spanish_ci | utf8mb4 | 231 | | Yes | 8 | 86 | utf8mb4_swedish_ci | utf8mb4 | 232 | | Yes | 8 | 87 | utf8mb4_turkish_ci | utf8mb4 | 233 | | Yes | 8 | 88 | utf8mb4_czech_ci | utf8mb4 | 234 | | Yes | 8 | 89 | utf8mb4_danish_ci | utf8mb4 | 235 | | Yes | 8 | 90 | utf8mb4_lithuanian_ci | utf8mb4 | 236 | | Yes | 8 | 91 | utf8mb4_slovak_ci | utf8mb4 | 237 | | Yes | 8 | 92 | utf8mb4_spanish2_ci | utf8mb4 | 238 | | Yes | 8 | 93 | utf8mb4_roman_ci | utf8mb4 | 239 | | Yes | 8 | 94 | utf8mb4_persian_ci | utf8mb4 | 240 | | Yes | 8 | 95 | utf8mb4_esperanto_ci | utf8mb4 | 241 | | Yes | 8 | 96 | utf8mb4_hungarian_ci | utf8mb4 | 242 | | Yes | 8 | 97 | utf8mb4_sinhala_ci | utf8mb4 | 243 | | Yes | 8 | 98 +--------------------------+---------+-----+---------+----------+---------+
数据库的常见操作
(1)查看数据库,查看数据创建的语句
1 --查看全部数据库 2 SHOW DATABASES; 3 --查看数据库的创建语句 4 SHOW CREATE DATABASE mydb1;
(2) 选择使用的数据库,查看正在使用的数据库
1 --选择正在使用数据库 2 SELECT DATABASE(); 3 --使用数据库 4 USE mydb2;
(3)修改数据库和删除数据库
修改数据库mydb2的校对规则为utf8_spanish_ci,然后查看建表语句发现已修改。
1 alter database mydb2 character set utf8 collate utf8_spanish_ci;
删除数据库语句比较简单。
1 --删除数据库 2 DROP DATABASE IF EXISTS mydb3;--IF EXISTS是可选的参数
可以看出有一个警告,是因为没有这个数据库,所以报警告。
数据库表的操作
关于数据库表的操作,分为两大块来记录,一块是针对表的创建、修改、删除,一块是针对表的增删改查操作,即数据库的CRUD(select,insert,update,delete)。
表的DDL操作
(1)创建表,表中字段类型可以通过查看文档获得。
1 CREATE TABLE employee( 2 id int primary key auto_increment, 3 name varchar(10), 4 gender char(2), 5 birthday date, 6 entry_date date, 7 job varchar(20), 8 salary double, 9 resume text 10 );
(2)修改表
按照如下要求修改上面创建的表。
1 --增加一个字段 2 ALTER TABLE employee ADD image blob; 3 --修改一个字段 4 ALTER TABLE employee MODIFY job varchar(60); 5 --删除字段 6 ALTER TABLE employee DROP gender; 7 --修改表名 8 RENAME TABLE employee TO emp; 9 --修改列名 10 ALTER TABLE emp CHANGE name username varchar(20); 11 --修改表的字符集 12 ALTER TABLE emp CHARACTER SET utf8;
1 mysql> desc employee; 2 +------------+-------------+------+-----+---------+----------------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +------------+-------------+------+-----+---------+----------------+ 5 | id | int(11) | NO | PRI | NULL | auto_increment | 6 | name | varchar(10) | YES | | NULL | | 7 | gender | char(2) | YES | | NULL | | 8 | birthday | date | YES | | NULL | | 9 | entry_date | date | YES | | NULL | | 10 | job | varchar(20) | YES | | NULL | | 11 | salary | double | YES | | NULL | | 12 | resume | text | YES | | NULL | | 13 +------------+-------------+------+-----+---------+----------------+ 14 8 rows in set (0.00 sec) 15 16 mysql> alter table employee add image blob; 17 Query OK, 0 rows affected (0.20 sec) 18 Records: 0 Duplicates: 0 Warnings: 0 19 20 mysql> desc employee; 21 +------------+-------------+------+-----+---------+----------------+ 22 | Field | Type | Null | Key | Default | Extra | 23 +------------+-------------+------+-----+---------+----------------+ 24 | id | int(11) | NO | PRI | NULL | auto_increment | 25 | name | varchar(10) | YES | | NULL | | 26 | gender | char(2) | YES | | NULL | | 27 | birthday | date | YES | | NULL | | 28 | entry_date | date | YES | | NULL | | 29 | job | varchar(20) | YES | | NULL | | 30 | salary | double | YES | | NULL | | 31 | resume | text | YES | | NULL | | 32 | image | blob | YES | | NULL | | 33 +------------+-------------+------+-----+---------+----------------+ 34 9 rows in set (0.00 sec) 35 36 mysql> alter table employee modify job varchar(60); 37 Query OK, 0 rows affected (0.17 sec) 38 Records: 0 Duplicates: 0 Warnings: 0 39 40 mysql> desc employee; 41 +------------+-------------+------+-----+---------+----------------+ 42 | Field | Type | Null | Key | Default | Extra | 43 +------------+-------------+------+-----+---------+----------------+ 44 | id | int(11) | NO | PRI | NULL | auto_increment | 45 | name | varchar(10) | YES | | NULL | | 46 | gender | char(2) | YES | | NULL | | 47 | birthday | date | YES | | NULL | | 48 | entry_date | date | YES | | NULL | | 49 | job | varchar(60) | YES | | NULL | | 50 | salary | double | YES | | NULL | | 51 | resume | text | YES | | NULL | | 52 | image | blob | YES | | NULL | | 53 +------------+-------------+------+-----+---------+----------------+ 54 9 rows in set (0.00 sec) 55 56 mysql> alter table employee drop gender; 57 Query OK, 0 rows affected (0.28 sec) 58 Records: 0 Duplicates: 0 Warnings: 0 59 60 mysql> desc employee; 61 +------------+-------------+------+-----+---------+----------------+ 62 | Field | Type | Null | Key | Default | Extra | 63 +------------+-------------+------+-----+---------+----------------+ 64 | id | int(11) | NO | PRI | NULL | auto_increment | 65 | name | varchar(10) | YES | | NULL | | 66 | birthday | date | YES | | NULL | | 67 | entry_date | date | YES | | NULL | | 68 | job | varchar(60) | YES | | NULL | | 69 | salary | double | YES | | NULL | | 70 | resume | text | YES | | NULL | | 71 | image | blob | YES | | NULL | | 72 +------------+-------------+------+-----+---------+----------------+ 73 8 rows in set (0.00 sec) 74 75 mysql> rename table employee emp; 76 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 77 corresponds to your MySQL server version for the right syntax to use near ‘emp‘ 78 at line 1 79 mysql> rename table employee to emp; 80 Query OK, 0 rows affected (0.08 sec) 81 82 mysql> show tables; 83 +-----------------+ 84 | Tables_in_mydb2 | 85 +-----------------+ 86 | emp | 87 +-----------------+ 88 1 row in set (0.00 sec) 89 90 mysql> desc emp; 91 +------------+-------------+------+-----+---------+----------------+ 92 | Field | Type | Null | Key | Default | Extra | 93 +------------+-------------+------+-----+---------+----------------+ 94 | id | int(11) | NO | PRI | NULL | auto_increment | 95 | name | varchar(10) | YES | | NULL | | 96 | birthday | date | YES | | NULL | | 97 | entry_date | date | YES | | NULL | | 98 | job | varchar(60) | YES | | NULL | | 99 | salary | double | YES | | NULL | | 100 | resume | text | YES | | NULL | | 101 | image | blob | YES | | NULL | | 102 +------------+-------------+------+-----+---------+----------------+ 103 8 rows in set (0.00 sec) 104 105 mysql> alter table emp change name username varchar(30); 106 Query OK, 0 rows affected (0.23 sec) 107 Records: 0 Duplicates: 0 Warnings: 0 108 109 mysql> desc emp; 110 +------------+-------------+------+-----+---------+----------------+ 111 | Field | Type | Null | Key | Default | Extra | 112 +------------+-------------+------+-----+---------+----------------+ 113 | id | int(11) | NO | PRI | NULL | auto_increment | 114 | username | varchar(30) | YES | | NULL | | 115 | birthday | date | YES | | NULL | | 116 | entry_date | date | YES | | NULL | | 117 | job | varchar(60) | YES | | NULL | | 118 | salary | double | YES | | NULL | | 119 | resume | text | YES | | NULL | | 120 | image | blob | YES | | NULL | | 121 +------------+-------------+------+-----+---------+----------------+ 122 8 rows in set (0.01 sec) 123 124 mysql> alter table emp character set utf8; 125 Query OK, 0 rows affected (0.22 sec) 126 Records: 0 Duplicates: 0 Warnings: 0 127 128 mysql> show create table emp; 129 +-------+----------------------------------------------------------------------- 130 -------------------------------------------------------------------------------- 131 -------------------------------------------------------------------------------- 132 -------------------------------------------------------------------------------- 133 -------------------------------------------------------------------------------- 134 --------------------------------------------------------------+ 135 | Table | Create Table 136 137 138 139 140 | 141 +-------+----------------------------------------------------------------------- 142 -------------------------------------------------------------------------------- 143 -------------------------------------------------------------------------------- 144 -------------------------------------------------------------------------------- 145 -------------------------------------------------------------------------------- 146 --------------------------------------------------------------+ 147 | emp | CREATE TABLE `emp` ( 148 `id` int(11) NOT NULL AUTO_INCREMENT, 149 `username` varchar(30) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL 150 , 151 `birthday` date DEFAULT NULL, 152 `entry_date` date DEFAULT NULL, 153 `job` varchar(60) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL, 154 `salary` double DEFAULT NULL, 155 `resume` text CHARACTER SET utf8 COLLATE utf8_spanish_ci, 156 `image` blob, 157 PRIMARY KEY (`id`) 158 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 159 +-------+----------------------------------------------------------------------- 160 -------------------------------------------------------------------------------- 161 -------------------------------------------------------------------------------- 162 -------------------------------------------------------------------------------- 163 -------------------------------------------------------------------------------- 164 --------------------------------------------------------------+ 165 1 row in set (0.00 sec)
(3)删除表
1 DROP TABLE emp;
另外还有一种删除表的方式,使用truncate关键字。一般来说truncate删除表效率高,相比delete from来说就是推倒重建,主键初始化等,工作中不推荐使用。
1 --删除表中所有记录 2 DELETE FROM employee; 3 --truncate删除方式,将表格重建,索引重新开始,如果是delete删除,索引继续按照上次记录开始 4 truncate employee; 5 --truncate删除效率要高于delete,并且其删除可能会破坏和其他表的关系,不推荐使用。
表的CRUD操作
(1)插入数据
1 --插入数据 字符串和日期类型数据需使用单引号括起来,字段自动增长,则这个字段不需要填写具体值。 2 INSERT INTO employee VALUES(null,‘zhangsan‘,‘M‘,‘1999-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘are you ok?‘); 3 INSERT INTO employee VALUES(null,‘lisi‘,‘M‘,‘1998-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘hello‘); 4 INSERT INTO employee VALUES(null,‘wangwu‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘buddy‘); 5 --auto_increment设置后,每次自增数据需要检查表中最大的数,然后在最大值的基础上加1。 6 --单独插入选定列,数据和列需要一一对应,如果没写任何字段,就是默认值插入。 7 INSERT INTO employee(id,name) VALUES(null,‘zhaoliu‘); 8 --插入中文,发现报错 9 --ERROR 1366 (HY000): Incorrect string value: ‘\\xD5\\xC5\\xC8\\xFD‘ for column ‘name‘ at row 1 10 INSERT INTO employee VALUES(null,‘张三‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘buddy‘);
1 mysql> show variables like ‘%character%‘; 2 +--------------------------+--------------------------+ 3 | Variable_name | Value | 4 +--------------------------+--------------------------+ 5 | character_set_client | utf8 | 6 | character_set_connection | utf8 | 7 | character_set_database | gbk | 8 | character_set_filesystem | binary | 9 | character_set_results | utf8 | 10 | character_set_server | utf8 | 11 | character_set_system | utf8 | 12 | character_sets_dir | D:\\mysql\\share\\charsets\\ | 13 +--------------------------+--------------------------+ 14 8 rows in set (0.00 sec) 15 16 mysql> INSERT INTO employee VALUES(null,‘zhangsan‘,‘M‘,‘1999-01-02‘,‘2000-01-01‘ 17 ,‘dance‘,15000,‘are you ok?‘); 18 Query OK, 1 row affected (0.08 sec) 19 20 mysql> INSERT INTO employee VALUES(null,‘lisi‘,‘M‘,‘1998-01-02‘,‘2000-01-01‘,‘d 21 ance‘,15000,‘hello‘); 22 Query OK, 1 row affected (0.03 sec) 23 24 mysql> INSERT INTO employee VALUES(null,‘wangwu‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘, 25 ‘dance‘,15000,‘buddy‘); 26 Query OK, 1 row affected (0.07 sec) 27 28 mysql> select * from employee; 29 +----+----------+--------+------------+------------+-------+--------+----------- 30 --+ 31 | id | name | gender | birthday | entry_date | job | salary | resume 32 | 33 +----+----------+--------+------------+------------+-------+--------+----------- 34 --+ 35 | 14 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 15000 | are you ok 36 ? | 37 | 15 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 15000 | hello 38 | 39 | 16 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 40 | 41 +----+----------+--------+------------+------------+-------+--------+----------- 42 --+ 43 3 rows in set (0.00 sec) 44 45 mysql> INSERT INTO employee(id,name) VALUES(null,‘zhaoliu‘); 46 Query OK, 1 row affected (0.07 sec) 47 48 mysql> select * from employee; 49 +----+----------+--------+------------+------------+-------+--------+----------- 50 --+ 51 | id | name | gender | birthday | entry_date | job | salary | resume 52 | 53 +----+----------+--------+------------+------------+-------+--------+----------- 54 --+ 55 | 14 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 15000 | are you ok 56 ? | 57 | 15 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 15000 | hello 58 | 59 | 16 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 60 | 61 | 17 | zhaoliu | NULL | NULL | NULL | NULL | NULL | NULL 62 | 63 +----+----------+--------+------------+------------+-------+--------+----------- 64 --+ 65 4 rows in set (0.00 sec) 66 67 mysql> INSERT INTO employee VALUES(null,‘张三‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘da 68 nce‘,15000,‘buddy‘); 69 ERROR 1366 (HY000): Incorrect string value: ‘\\xD5\\xC5\\xC8\\xFD‘ for column ‘name‘ 70 at row 1
发现在插入中文字符时报错,本质原因就是编码和解码的规则不一致造成的,mysql client窗口默认是gbk,窗口向mysql client传输使用的是utf8,mysql client使用的是utf8,返回给mysql client使用的是utf-8,因此造成了编码解码的不一致。解决这个问题有临时策略和永久策略,临时策略就是修改mysql client窗口的字符集为utf8。
临时策略,只在当前窗口有效
1 mysql> show variables like ‘%character%‘; 2 +--------------------------+--------------------------+ 3 | Variable_name | Value | 4 +--------------------------+--------------------------+ 5 | character_set_client | utf8 | 6 | character_set_connection | utf8 | 7 | character_set_database | gbk | 8 | character_set_filesystem | binary | 9 | character_set_results | utf8 | 10 | character_set_server | utf8 | 11 | character_set_system | utf8 | 12 | character_sets_dir | D:\\mysql\\share\\charsets\\ | 13 +--------------------------+--------------------------+ 14 8 rows in set (0.00 sec) 15 16 mysql> set names gbk; 17 Query OK, 0 rows affected (0.00 sec) 18 19 mysql> show variables like ‘%character%‘; 20 +--------------------------+--------------------------+ 21 | Variable_name | Value | 22 +--------------------------+--------------------------+ 23 | character_set_client | gbk | 24 | character_set_connection | gbk | 25 | character_set_database | gbk | 26 | character_set_filesystem | binary | 27 | character_set_results | gbk | 28 | character_set_server | utf8 | 29 | character_set_system | utf8 | 30 | character_sets_dir | D:\\mysql\\share\\charsets\\ | 31 +--------------------------+--------------------------+ 32 8 rows in set (0.00 sec) 33 34 mysql> select * from employee; 35 +----+----------+--------+------------+------------+-------+--------+----------- 36 --+ 37 | id | name | gender | birthday | entry_date | job | salary | resume 38 | 39 +----+----------+--------+------------+------------+-------+--------+----------- 40 --+ 41 | 14 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 15000 | are you ok 42 ? | 43 | 15 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 15000 | hello 44 | 45 | 16 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 46 | 47 | 17 | zhaoliu | NULL | NULL | NULL | NULL | NULL | NULL 48 | 49 +----+----------+--------+------------+------------+-------+--------+----------- 50 --+ 51 4 rows in set (0.00 sec) 52 53 mysql> INSERT INTO employee VALUES(null,‘张三‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘da 54 nce‘,15000,‘buddy‘); 55 Query OK, 1 row affected (0.07 sec) 56 57 mysql> select * from employee; 58 +----+----------+--------+------------+------------+-------+--------+----------- 59 --+ 60 | id | name | gender | birthday | entry_date | job | salary | resume 61 | 62 +----+----------+--------+------------+------------+-------+--------+----------- 63 --+ 64 | 14 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 15000 | are you ok 65 ? | 66 | 15 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 15000 | hello 67 | 68 | 16 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 69 | 70 | 17 | zhaoliu | NULL | NULL | NULL | NULL | NULL | NULL 71 | 72 | 18 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 73 | 74 +----+----------+--------+------------+------------+-------+--------+----------- 75 --+ 76 5 rows in set (0.00 sec)
永久策略,修改mysql配置文件my.ini,并重启mysql服务即可。
重启可以在dos下,也可以在服务中来重启。
1 #关闭mysql 2 net stop mysql 3 #开启mysql 4 net start mysql
重启后查看字符集,并继续插入带中文OK,这种配置是永久的。
1 mysql> use mydb2; 2 Database changed 3 mysql> show variables like ‘%character%‘; 4 +--------------------------+--------------------------+ 5 | Variable_name | Value | 6 +--------------------------+--------------------------+ 7 | character_set_client | gbk | 8 | character_set_connection | gbk | 9 | character_set_database | gbk | 10 | character_set_filesystem | binary | 11 | character_set_results | gbk | 12 | character_set_server | utf8 | 13 | character_set_system | utf8 | 14 | character_sets_dir | D:\\mysql\\share\\charsets\\ | 15 +--------------------------+--------------------------+ 16 8 rows in set (0.00 sec) 17 18 mysql> select * from employee; 19 Empty set (0.00 sec) 20 21 mysql> show variables like ‘%character%‘; 22 +--------------------------+--------------------------+ 23 | Variable_name | Value | 24 +--------------------------+--------------------------+ 25 | character_set_client | gbk | 26 | character_set_connection | gbk | 27 | character_set_database | gbk | 28 | character_set_filesystem | binary | 29 | character_set_results | gbk | 30 | character_set_server | utf8 | 31 | character_set_system | utf8 | 32 | character_sets_dir | D:\\mysql\\share\\charsets\\ | 33 +--------------------------+--------------------------+ 34 8 rows in set (0.00 sec) 35 36 mysql> INSERT INTO employee VALUES(null,‘张三‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘da 37 nce‘,15000,‘buddy‘); 38 Query OK, 1 row affected (0.08 sec) 39 40 mysql> select * from employee; 41 +----+------+--------+------------+------------+-------+--------+--------+ 42 | id | name | gender | birthday | entry_date | job | salary | resume | 43 +----+------+--------+------------+------------+-------+--------+--------+ 44 | 19 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy | 45 +----+------+--------+------------+------------+-------+--------+--------+ 46 1 row in set (0.00 sec)
(2)更新数据
更新语句参考如下格式。
1 --基本语句 2 UPDATE employee set salary=5000; 3 --通过where条件,添加过滤条件 4 UPDATE employee set salary=10000 WHERE id=1; 5 --set语句中也可以写表达式 6 UPDATE employee set salary=salary+5000 WHERE name=‘zhangsan‘; UPDATE employee set salary=6000,job=‘driver‘ WHERE name=‘lisi‘;
1 mysql> select * from employee; 2 +----+----------+--------+------------+------------+-------+--------+----------- 3 --+ 4 | id | name | gender | birthday | entry_date | job | salary | resume 5 | 6 +----+----------+--------+------------+------------+-------+--------+----------- 7 --+ 8 | 19 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 9 | 10 | 20 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 15000 | are you ok 11 ? | 12 | 21 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 15000 | hello 13 | 14 | 22 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 15000 | buddy 15 | 16 | 23 | zhaoliu | NULL | NULL | NULL | NULL | NULL | NULL 17 | 18 +----+----------+--------+------------+------------+-------+--------+----------- 19 --+ 20 5 rows in set (0.00 sec) 21 22 mysql> update employee set salary=5000; 23 Query OK, 5 rows affected (0.07 sec) 24 Rows matched: 5 Changed: 5 Warnings: 0 25 26 mysql> select * from employee; 27 +----+----------+--------+------------+------------+-------+--------+----------- 28 --+ 29 | id | name | gender | birthday | entry_date | job | salary | resume 30 | 31 +----+----------+--------+------------+------------+-------+--------+----------- 32 --+ 33 | 19 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 5000 | buddy 34 | 35 | 20 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 5000 | are you ok 36 ? | 37 | 21 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 5000 | hello 38 | 39 | 22 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 5000 | buddy 40 | 41 | 23 | zhaoliu | NULL | NULL | NULL | NULL | 5000 | NULL 42 | 43 +----+----------+--------+------------+------------+-------+--------+----------- 44 --+ 45 5 rows in set (0.00 sec) 46 47 mysql> update employee set salary=18888 where id=19; 48 Query OK, 1 row affected (0.03 sec) 49 Rows matched: 1 Changed: 1 Warnings: 0 50 51 mysql> select * from employee; 52 +----+----------+--------+------------+------------+-------+--------+----------- 53 --+ 54 | id | name | gender | birthday | entry_date | job | salary | resume 55 | 56 +----+----------+--------+------------+------------+-------+--------+----------- 57 --+ 58 | 19 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 18888 | buddy 59 | 60 | 20 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 5000 | are you ok 61 ? | 62 | 21 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 5000 | hello 63 | 64 | 22 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 5000 | buddy 65 | 66 | 23 | zhaoliu | NULL | NULL | NULL | NULL | 5000 | NULL 67 | 68 +----+----------+--------+------------+------------+-------+--------+----------- 69 --+ 70 5 rows in set (0.00 sec) 71 72 mysql> update employee set salary=salary+5000 where name=‘zhangsan‘; 73 Query OK, 1 row affected (0.08 sec) 74 Rows matched: 1 Changed: 1 Warnings: 0 75 76 mysql> select * from employee; 77 +----+----------+--------+------------+------------+-------+--------+----------- 78 --+ 79 | id | name | gender | birthday | entry_date | job | salary | resume 80 | 81 +----+----------+--------+------------+------------+-------+--------+----------- 82 --+ 83 | 19 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 18888 | buddy 84 | 85 | 20 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 10000 | are you ok 86 ? | 87 | 21 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 5000 | hello 88 | 89 | 22 | wangwu | M | 1990-01-02 | 2000-01-01 | dance | 5000 | buddy 90 | 91 | 23 | zhaoliu | NULL | NULL | NULL | NULL | 5000 | NULL 92 | 93 +----+----------+--------+------------+------------+-------+--------+----------- 94 --+ 95 5 rows in set (0.00 sec) 96 97 mysql> update employee set salary=8888,job=‘old driver‘ where name=‘wangwu‘; 98 Query OK, 1 row affected (0.05 sec) 99 Rows matched: 1 Changed: 1 Warnings: 0 100 101 mysql> select * from employee; 102 +----+----------+--------+------------+------------+------------+--------+------ 103 -------+ 104 | id | name | gender | birthday | entry_date | job | salary | resum 105 e | 106 +----+----------+--------+------------+------------+------------+--------+------ 107 -------+ 108 | 19 | 张三 | M | 1990-01-02 | 2000-01-01 | dance | 18888 | buddy 109 | 110 | 20 | zhangsan | M | 1999-01-02 | 2000-01-01 | dance | 10000 | are y 111 ou ok? | 112 | 21 | lisi | M | 1998-01-02 | 2000-01-01 | dance | 5000 | hello 113 | 114 | 22 | wangwu | M | 1990-01-02 | 2000-01-01 | old driver | 8888 | buddy 115 | 116 | 23 | zhaoliu | NULL | NULL | NULL | NULL | 5000 | NULL 117 | 118 +----+----------+--------+------------+------------+------------+--------+------ 119 -------+ 120 5 rows in set (0.00 sec)
(3)删除数据
1 --普通语句 2 DELETE FROM employee WHERE name=‘zhaoliu‘ AND id=4; --删除表中所有记录 3 DELETE FROM employee; 4 --truncate删除方式,将表格重建,索引重新开始,如果是delete删除,索引继续按照上次记录开始 5 truncate employee; 6 --truncate删除效率要高于delete,并且其删除可能会破坏和其他表的关系,不推荐使用。
(4)查询数据
普通查询,可以添加distinct关键字去重。
1 --普通查询 2 SELECT id,name FROM employee WHERE id=‘1‘; --DISTINCT去重,下面是所有的都不能重复 3 SELECT DISTINCT * FROM employee; 4 SELECT DISTINCT name FROM employee;
先创建一个表进行测试。
1 create table exam( 2 id int primary key auto_increment, 3 name varchar(20) not null, 4 chinese double, 5 math double, 6 english double 7 ); 8 insert into exam values(null,‘关羽‘,85,76,70); 9 insert into exam values(null,‘张飞‘,70,75,70); 10 insert into exam values(null,‘赵云‘,90,65,95);
开始执行以下操作,包括常用基本查询。
1 --查询名字和英文成绩 2 SELECT name,english FROM exam; 3 --english去重操作 4 SELECT DISTINCT english FROM exam; 5 --可以使用表达式对查询结果进行运算 6 SELECT name,chinese+10,math+10,english+10 FROM exam; 7 --可以使用别名,用AS或者空格均可以,但是空格不建议使用 8 SELECT name,chinese+10 chinese,math+10 AS math,english+10 AS english FROM exam; 9 --统计每个学生总分 10 SELECT name,chinese+math+english FROM exam; SELECT name,chinese+math+english AS sumscore FROM exam; 11 --查询姓名为张飞的学生成绩 12 SELECT name,chinese,math,english FROM exam WHERE name=‘张飞‘; --查询英语成绩大于90分 13 SELECT * FROM exam WHERE english>90; --查询总分大于200分的 14 SELECT *, math+chinese+english as sumscore FROM exam WHERE (math+chinese+english)>230; 15 --查询英语在80-100之间同学 16 SELECT * FROM exam WHERE english BETWEEN 80 AND 100; 17 --查询数学分数为75,76,77的同学 18 SELECT * FROM exam WHERE math in(75,76,77); 19 --查询所有姓张同学的成绩 20 SELECT * FROM exam WHERE name LIKE ‘张%‘; 21 --查询数学大于70,语文大于80的同学 22 SELECT * FROM exam WHERE math>70 AND chinese>80; 23 --查询数学不低于60分的 24 SELECT * FROM exam WHERE NOT(math<60); 25 --查询数学成绩缺考的同学 26 SELECT * FROM exam WHERE math IS NULL; 27 --查询所有人的数学成绩,如果为null,用0分展示 28 SELECT ifnull(math,0) AS math FROM exam;
1 mysql> select * from exam; 2 +----+------+---------+------+---------+ 3 | id | name | chinese | math | english | 4 +----+------+---------+------+---------+ 5 | 1 | 关羽 | 85 | 76 | 70 | 6 | 2 | 张飞 | 70 | 75 | 70 | 7 | 3 | 赵云 | 90 | 65 | 95 | 8 +----+------+---------+------+---------+ 9 3 rows in set (0.00 sec) 10 11 mysql> SELECT name,english FROM exam; 12 +------+---------+ 13 | name | english | 14 +------+---------+ 15 | 关羽 | 70 | 16 | 张飞 | 70 | 17 | 赵云 | 95 | 18 +------+---------+ 19 3 rows in set (0.00 sec) 20 21 mysql> SELECT DISTINCT english FROM exam; 22 +---------+ 23 | english | 24 +---------+ 25 | 70 | 26 | 95 | 27 +---------+ 28 2 rows in set (0.00 sec) 29 30 mysql> SELECT name,chinese+10,math+10,english+10 FROM exam; 31 +------+------------+---------+------------+ 32 | name | chinese+10 | math+10 | english+10 | 33 +------+------------+---------+------------+ 34 | 关羽 | 95 | 86 | 80 | 35 | 张飞 | 80 | 85 | 80 | 36 | 赵云 | 100 | 75 | 105 | 37 +------+------------+---------+------------+ 38 3 rows in set (0.00 sec) 39 40 mysql> SELECT name,chinese+10 chinese,math+10 AS math,english+10 AS english FROM 41 exam; 42 +------+---------+------+---------+ 43 | name | chinese | math | english | 44 +------+---------+------+---------+ 45 | 关羽 | 95 | 86 | 80 | 46 | 张飞 | 80 | 85 | 80 | 47 | 赵云 | 100 | 75 | 105 | 48 +------+---------+------+---------+ 49 3 rows in set (0.00 sec) 50 51 mysql> SELECT name,chinese+math+english AS sumscore FROM exam; 52 +------+----------+ 53 | name | sumscore | 54 +------+----------+ 55 | 关羽 | 231 | 56 | 张飞 | 215 | 57 | 赵云 | 250 | 58 +------+----------+ 59 3 rows in set (0.00 sec) 60 61 mysql> SELECT name,chinese,math,english FROM exam WHERE name=‘张飞‘; 62 +------+---------+------+---------+ 63 | name | chinese | math | english | 64 +------+---------+------+---------+ 65 | 张飞 | 70 | 75 | 70 | 66 +------+---------+------+---------+ 67 1 row in set (0.02 sec) 68 69 mysql> SELECT * FROM exam WHERE english>90; 70 +----+------+---------+------+---------+ 71 | id | name | chinese | math | english | 72 +----+------+---------+------+---------+ 73 | 3 | 赵云 | 90 | 65 | 95 | 74 +----+------+---------+------+---------+ 75 1 row in set (0.00 sec) 76 77 mysql> SELECT *, math+chinese+english as sumscore FROM exam WHERE (math+chinese 78 +english)>230; 79 +----+------+---------+------+---------+----------+ 80 | id | name | chinese | math | english | sumscore | 81 +----+------+---------+------+---------+----------+ 82 | 1 | 关羽 | 85 | 76 | 70 | 231 | 83 | 3 | 赵云 | 90 | 65 | 95 | 250 | 84 +----+------+---------+------+---------+----------+ 85 2 rows in set (0.00 sec) 86 87 mysql> SELECT * FROM exam WHERE english BETWEEN 80 AND 100; 88 +----+------+---------+------+---------+ 89 | id | name | chinese | math | english | 90 +----+------+---------+------+---------+ 91 | 3 | 赵云 | 90 | 65 | 95 | 92 +----+------+---------+------+---------+ 93 1 row in set (0.06 sec) 94 95 mysql> SELECT * FROM exam WHERE math in(75,76,77); 96 +----+------+---------+------+---------+ 97 | id | name | chinese | math | english | 98 +----+------+---------+------+---------+ 99 | 1 | 关羽 | 85 | 76 | 70 | 100 | 2 | 张飞 | 70 | 75 | 70 | 101 +----+------+---------+------+---------+ 102 2 rows in set (0.00 sec) 103 104 mysql> SELECT * FROM exam WHERE name LIKE ‘张%‘; 105 +----+------+---------+------+---------+ 106 | id | name | chinese | math | english | 107 +----+------+---------+------+---------+ 108 | 2 | 张飞 | 70 | 75 | 70 | 109 +----+------+---------+------+---------+ 110 1 row in set (0.00 sec) 111 112 mysql> SELECT * FROM exam WHERE math>70 AND chinese>80; 113 +----+------+---------+------+---------+ 114 | id | name | chinese | math | english | 115 +----+------+---------+------+---------+ 116 | 1 | 关羽 | 85 | 76 | 70 | 117 +----+------+---------+------+---------+ 118 1 row in set (0.00 sec) 119 120 mysql> SELECT * FROM exam WHERE NOT(math<60); 121 +----+------+---------+------+---------+ 122 | id | name | chinese | math | english | 123 +----+------+---------+------+---------+ 124 | 1 | 关羽 | 85 | 76 | 70 | 125 | 2 | 张飞 | 70 | 75 | 70 | 126 | 3 | 赵云 | 90 | 65 | 95 | 127 +----+------+---------+------+---------+ 128 3 rows in set (0.00 sec) 129 130 mysql> SELECT * FROM exam WHERE math IS NULL; 131 Empty set (0.00 sec) 132 133 mysql> SELECT ifnull(math,0) AS math FROM exam; 134 +------+ 135 | math | 136 +------+ 137 | 76 | 138 | 75 | 139 | 65 | 140 +------+ 141 3 rows in set (0.00 sec) 142 143 mysql> select * from exam; 144 +----+------+---------+------+---------+ 145 | id | name | chinese | math | english | 146 +----+------+---------+------+---------+ 147 | 1 | 关羽 | 85 | 76 | 70 | 148 | 2 | 张飞 | 70 | 75 | 70 | 149 | 3 | 赵云 | 90 | 65 | 95 | 150 +----+------+---------+------+---------+ 151 3 rows in set (0.00 sec) 152 153 mysql> insert into exam values(null,"刘备",null,null,99); 154 Query OK, 1 row affected (0.08 sec) 155 156 mysql> select * from exam; 157 +----+------+---------+------+---------+ 158 | id | name | chinese | math | english | 159 +----+------+---------+------+---------+ 160 | 1 | 关羽 | 85 | 76 | 70 | 161 | 2 | 张飞 | 70 | 75 | 70 | 162 | 3 | 赵云 | 90 | 65 | 95 | 163 | 4 | 刘备 | NULL | NULL | 99 | 164 +----+------+---------+------+---------+ 165 4 rows in set (0.00 sec) 166 167 mysql> SELECT * FROM exam WHERE math IS NULL; 168 +----+------+---------+------+---------+ 169 | id | name | chinese | math | english | 170 +----+------+---------+------+---------+ 171 | 4 | 刘备 | NULL | NULL | 99 | 172 +----+------+---------+------+---------+ 173 1 row in set (0.00 sec) 174 175 mysql> SELECT ifnull(math,0) AS math FROM exam; 176 +------+ 177 | math | 178 +------+ 179 | 76 | 180 | 75 | 181 | 65 | 182 | 0 | 183 +------+ 184 4 rows in set (0.00 sec)
ORDER BY的使用
1 使用order by子句排序,order by永远放在sql语句的最后面,执行顺序也是最后面 2 --对语文成绩排序后输出 3 SELECT * FROM exam ORDER BY chinese; 4 --对总分排序按照从高到低 5 SELECT ifnull(chinese,0)+ifnull(math,0)+ifnull(english,0) as sumscore FROM exam ORDER BY sumscore DESC; 6 --对姓李的学生成绩排序输出 7 SELECT * FROM exam WHERE name LIKE ‘张%‘ ORDER BY chinese;
1 mysql> SELECT * FROM exam ORDER BY chinese; 2 +----+------+---------+------+---------+ 3 | id | name | chinese | math | english | 4 +----+------+---------+------+---------+ 5 | 4 | 刘备 | NULL | NULL | 99 | 6 | 2 | 张飞 | 70 | 75 | 70 | 7 | 1 | 关羽 | 85 | 76 | 70 | 8 | 3 | 赵云 | 90 | 65 | 95 | 9 +----+------+---------+------+---------+ 10 4 rows in set (0.00 sec) 11 12 mysql> SELECT ifnull(chinese,0)+ifnull(math,0)+ifnull(english,0) as sumscore FRO 13 M exam ORDER BY sumscore DESC; 14 +----------+ 15 | sumscore | 16 +----------+ 17 | 250 | 18 | 231 | 19 | 215 | 20 | 99 | 21 +----------+ 22 4 rows in set (0.00 sec) 23 24 mysql> SELECT * FROM exam WHERE name LIKE ‘张%‘ ORDER BY chinese; 25 +----+------+---------+------+---------+ 26 | id | name | chinese | math | english | 27 +----+------+---------+------+---------+ 28 | 2 | 张飞 | 70 | 75 | 70 | 29 +----+------+---------+------+---------+ 30 1 row in set (0.00 sec)
聚集函数的使用,包括COUNT()、SUM()、AVG()、MAX()/MIN()等
1 --COUNT() 对所有满足条件的行进行计数操作,count里可以写*或者字段 2 --统计一个班级共有多少学生? 3 SELECT COUNT(*) AS COUNT FROM exam; 4 --统计数学成绩大于90的学生有多少个? 5 SELECT COUNT(*) AS COUNT FROM exam WHERE math>90; 6 --统计总分大于250的人数有多少? 7 SELECT COUNT(name) AS COUNT FROM exam WHERE ifnull(chinese,0)+ifnull(math,0)+ifnull(english,0)>250; 8 --SUM() 将满足条件的行,对指定字段求和,只对数值起作用,多列逗号不能省。 9 --统计一个班级数学总成绩? 10 SELECT SUM(ifnull(math,0)) FROM exam;--null不统计,可以不写 11 SELECT SUM(math) FROM exam; 12 --统计一个班级语文、英语、数学各科的总成绩 13 SELECT SUM(chinese),SUM(english),SUM(math) FROM exam; 14 --统计一个班级语文、英语、数学的成绩总和 15 SELECT SUM(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) FROM exam; 16 --统计一个班级语文成绩平均分 17 SELECT SUM(chinese)/count(chinese) FROM exam; 18 --AVG() 对于满足条件的行,对指定字段进行求平均数 19 SELECT AVG(math) FROM exam;--null不统计 20 --求一个班级总分平均分? 21 SELECT AVG(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) FROM exam; 22 --MAX/MIN 返回满足条件的行,里面指定字段内最大最小的值 23 --求出语文最高分 24 SELECT MAX(chinese) FROM exam;
1 mysql> SELECT COUNT(*) AS COUNT FROM exam; 2 +-------+ 3 | COUNT | 4 +-------+ 5 | 4 | 6 +-------+ 7 1 row in set (0.00 sec) 8 9 mysql> SELECT COUNT(*) AS COUNT FROM exam WHERE math>90; 10 +-------+ 11 | COUNT | 12 +-------+ 13 | 0 | 14 +-------+ 15 1 row in set (0.00 sec) 16 17 mysql> SELECT COUNT(name) AS COUNT FROM exam WHERE ifnull(chinese,0)+ifnull(math 18 ,0)+ifnull(english,0)>250; 19 +-------+ 20 | COUNT | 21 +-------+ 22 | 0 | 23 +-------+ 24 1 row in set (0.00 sec) 25 26 mysql> SELECT SUM(ifnull(math,0)) FROM exam; 27 +---------------------+ 28 | SUM(ifnull(math,0)) | 29 +---------------------+ 30 | 216 | 31 +---------------------+ 32 1 row in set (0.00 sec) 33 34 mysql> SELECT SUM(math) FROM exam; 35 +-----------+ 36 | SUM(math) | 37 +-----------+ 38 | 216 | 39 +-----------+ 40 1 row in set (0.00 sec) 41 42 mysql> SELECT SUM(chinese),SUM(english),SUM(math) FROM exam; 43 +--------------+--------------+-----------+ 44 | SUM(chinese) | SUM(english) | SUM(math) | 45 +--------------+--------------+-----------+ 46 | 245 | 334 | 216 | 47 +--------------+--------------+-----------+ 48 1 row in set (0.00 sec) 49 50 mysql> SELECT SUM(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) FROM exam; 51 52 +---------------------------------------------------------+ 53 | SUM(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) | 54 +---------------------------------------------------------+ 55 | 795 | 56 +---------------------------------------------------------+ 57 1 row in set (0.00 sec) 58 59 mysql> SELECT SUM(chinese)/count(chinese) FROM exam; 60 +-----------------------------+ 61 | SUM(chinese)/count(chinese) | 62 +-----------------------------+ 63 | 81.66666666666667 | 64 +-----------------------------+ 65 1 row in set (0.00 sec) 66 67 mysql> SELECT AVG(math) FROM exam; 68 +-----------+ 69 | AVG(math) | 70 +-----------+ 71 | 72 | 72 +-----------+ 73 1 row in set (0.00 sec) 74 75 mysql> SELECT AVG(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) FROM exam; 76 77 +---------------------------------------------------------+ 78 | AVG(ifnull(chinese,0)+ifnull(english,0)+ifnull(math,0)) | 79 +---------------------------------------------------------+ 80 | 198.75 | 81 +---------------------------------------------------------+ 82 1 row in set (0.00 sec) 83 84 mysql> SELECT MAX(chinese) FROM exam; 85 +--------------+ 86 | MAX(chinese) | 87 +--------------+ 88 | 90 | 89 +--------------+ 90 1 row in set (0.00 sec)
GROUP BY的使用
1 --根据指定字段值进行分组,字段值相同的分到同一组,并只显示分组后第一条数据。 2 --分组后的数据,只显示第一行,其他数据被覆盖不显示,但是数据没有被抛弃。 3 create table orders( 4 id int, 5 product varchar(20), 6 price float 7 ); 8 9 insert into orders(id,product,price) values(1,‘电视‘,900); 10 insert into orders(id,product,price) values(2,‘洗衣机‘,100); 11 insert into orders(id,product,price) values(3,‘洗衣粉‘,90); 12 insert into orders(id,product,price) values(4,‘桔子‘,9); 13 insert into orders(id,product,price) values(5,‘洗衣粉‘,90); --对订单表中商品归类后,显示每一类商品的总价 14 SELECT * FROM orders GROUP BY product;--分组后只显示一条洗衣粉,另外一条被覆盖 SELECT product, SUM(price) FROM orders GROUP BY product; 15 --查询购买了几类商品,并且每类总价大于100的商品 16 SELECT product, SUM(price) AS sumprice FROM orders GROUP BY product HAVING sumprice>100; --说明是先分完分组计算后在执行having
1 mysql> create table orders( 2 -> id int, 3 -> product varchar(20), 4 -> price float 5 -> ); 6 Query OK, 0 rows affected (0.11 sec) 7 8 mysql> 9 mysql> insert into orders(id,product,price) values(1,‘电视‘,900); 10 Query OK, 1 row affected (0.03 sec) 11 12 mysql> insert into orders(id,product,price) values(2,‘洗衣机‘,100); 13 Query OK, 1 row affected (0.02 sec) 14 15 mysql> insert into orders(id,product,price) values(3,‘洗衣粉‘,90); 16 Query OK, 1 row affected (0.03 sec) 17 18 mysql> insert into orders(id,product,price) values(4,‘桔子‘,9); 19 Query OK, 1 row affected (0.02 sec) 20 21 mysql> insert into orders(id,product,price) values(5,‘洗衣粉‘,90); 22 Query OK, 1 row affected (0.08 sec) 23 24 mysql> show tables; 25 +-----------------+ 26 | Tables_in_mydb2 | 27 +-----------------+ 28 | employee | 29 | exam | 30 | orders | 31 +-----------------+ 32 3 rows in set (0.00 sec) 33 34 mysql> select * from orders; 35 +------+---------+-------+ 36 | id | product | price | 37 +------+---------+-------+ 38 | 1 | 电视 | 900 | 39 | 2 | 洗衣机 | 100 | 40 | 3 | 洗衣粉 | 90 | 41 | 4 | 桔子 | 9 | 42 | 5 | 洗衣粉 | 90 | 43 +------+---------+-------+ 44 5 rows in set (0.00 sec) 45 46 mysql> SELECT * FROM orders GROUP BY product; 47 +------+---------+-------+ 48 | id | product | price | 49 +------+---------+-------+ 50 | 1 | 电视 | 900 | 51 | 4 | 桔子 | 9 | 52 | 3 | 洗衣粉 | 90 | 53 | 2 | 洗衣机 | 100 | 54 +------+---------+-------+ 55 4 rows in set (0.00 sec) 56 57 mysql> SELECT product, SUM(price) AS sumprice FROM orders GROUP BY product HAVI 58 G sumprice>100; 59 +---------+----------+ 60 | product | sumprice | 61 +---------+----------+ 62 | 电视 | 900 | 63 | 洗衣粉 | 180 | 64 +---------+----------+ 65 2 rows in set (0.00 sec)
外键约束
1 --外键约束 维护数据的完整性和正确性,但是在高吞吐量下不太建议使用 2 --foreign key(当前表字段名) references 另外一张表(另外一张表字段名) 3 create table dept( 4 id int primary key auto_increment, 5 name varchar(20) 6 ); 7 insert into dept values (null,‘财务部‘),(null,‘人事部‘),(null,‘科技部‘),(null,‘销售部‘); 8 create table emp( 9 id int primary key auto_increment, 10 name varchar(20), 11 dept_id int, 12 FOREIGN KEY(dept_id) REFERENCES dept(id) 13 ); 14 insert into emp values (null,‘张飞‘,1),(null,‘关羽‘,2),(null,‘刘备‘,3),(null,‘赵云‘,4);
1 mysql> create table dept( 2 -> id int primary key auto_increment, 3 -> name varchar(20) 4 5 -> ); 6 Query OK, 0 rows affected (0.12 sec) 7 8 mysql> insert into dept values (null,‘财务部‘),(null,‘人事部‘),(null,‘科技部 9 ‘),(null,‘销售部‘); 10 Query OK, 4 rows affected (0.03 sec) 11 Records: 4 Duplicates: 0 Warnings: 0 12 13 mysql> create table emp( 14 -> id int primary key auto_increment, 15 -> name varchar(20), 16 -> dept_id int, 17 -> FOREIGN KEY(dept_id) REFERENCES dept(id) 18 -> ); 19 Query OK, 0 rows affected (0.07 sec) 20 21 mysql> insert into emp values (null,‘张飞‘,1),(null,‘关羽‘,2),(null,‘刘备‘, 22 ),(null,‘赵云‘,4); 23 Query OK, 4 rows affected (0.07 sec) 24 Records: 4 Duplicates: 0 Warnings: 0 25 26 mysql> show tables; 27 +-----------------+ 28 | Tables_in_mydb2 | 29 +-----------------+ 30 | dept | 31 | emp | 32 | employee | 33 | exam | 34 | orders | 35 +-----------------+ 36 5 rows in set (0.00 sec) 37 38 mysql> select * from dept; 39 +----+--------+ 40 | id | name | 41 +----+--------+ 42 | 1 | 财务部 | 43 | 2 | 人事部 | 44 | 3 | 科技部 | 45 | 4 | 销售部 | 46 +----+--------+ 47 4 rows in set (0.00 sec) 48 49 mysql> select * from emp; 50 +----+------+---------+ 51 | id | name | dept_id | 52 +----+------+---------+ 53 | 1 | 张飞 | 1 | 54 | 2 | 关羽 | 2 | 55 | 3 | 刘备 | 3 | 56 | 4 | 赵云 | 4 | 57 +----+------+---------+ 58 4 rows in set (0.00 sec) 59 60 mysql> delete from dept where id=4; 61 ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constra 62 nt fails (`mydb2`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dept_id`) REFERE 63 CES `dept` (`id`)) 64 mysql> insert into emp values(null,‘hehe‘,5); 65 ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint 66 ails (`mydb2`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES 67 `dept` (`id`))
多表查询
1 --笛卡尔积查询:两张表相乘得出来的结果。如果左边表有m条记录,右边有n条记录,则查询出来的结果就是m*n条。这些查询结果中包含大量错误的结果,通常不会使用这种查询。 2 --关联查询 3 select * from emp,dept where emp.dept_id=dept.id; 4 --内连接查询 5 SELECT * FROM emp inner join dept ON emp.dept_id=dept.id; 6 --左外连接 , 以左边表为基础,如果没有关联结果就显示null 7 SELECT * FROM dept left outer join emp on dept.id=emp.dept_id; 8 --右外连接 9 SELECT * FROM dept right outer join emp on dept.id=emp.dept_id; 10 --全外连接 通过union关键字来模拟全外连接 11 SELECT * FROM dept left outer join emp on dept.id=emp.dept_id; 12 UNION 13 SELECT * FROM dept right outer join emp on dept.id=emp.dept_id;
1 mysql> select * from emp; 2 +----+------+---------+ 3 | id | name | dept_id | 4 +----+------+---------+ 5 | 1 | 张飞 | 1 | 6 | 2 | 关羽 | 2 | 7 | 3 | 刘备 | 3 | 8 | 4 | 赵云 | 4 | 9 +----+------+---------+ 10 4 rows in set (0.00 sec) 11 12 mysql> select * from dept; 13 +----+--------+ 14 | id | name | 15 +----+--------+ 16 | 1 | 财务部 | 17 | 2 | 人事部 | 18 | 3 | 科技部 | 19 | 4 | 销售部 | 20 +----+--------+ 21 4 rows in set (0.00 sec) 22 23 mysql> select * from emp,dept; 24 +----+------+---------+----+--------+ 25 | id | name | dept_id | id | name | 26 +----+------+---------+----+--------+ 27 | 1 | 张飞 | 1 | 1 | 财务部 | 28 | 2 | 关羽 | 2 | 1 | 财务部 | 29 | 3 | 刘备 | 3 | 1 | 财务部 | 30 | 4 | 赵云 | 4 | 1 | 财务部 | 31 | 1 | 张飞 | 1 | 2 | 人事部 | 32 | 2 | 关羽 | 2 | 2 | 人事部 | 33 | 3 | 刘备 | 3 | 2 | 人事部 | 34 | 4 | 赵云 | 4 | 2 | 人事部 | 35 | 1 | 张飞 | 1 | 3 | 科技部 | 36 | 2 | 关羽 | 2 | 3 | 科技部 | 37 | 3 | 刘备 | 3 | 3 | 科技部 | 38 | 4 | 赵云 | 4 | 3 | 科技部 | 39 | 1 | 张飞 | 1 | 4 | 销售部 | 40 | 2 | 关羽 | 2 | 4 | 销售部 | 41 | 3 | 刘备 | 3 | 4 | 销售部 | 42 | 4 | 赵云 | 4 | 4 | 销售部 | 43 +----+------+---------+----+--------+ 44 16 rows in set (0.00 sec) 45 46 mysql> select * from emp,dept where emp.dept_id=dept.id; 47 +----+------+---------+----+--------+ 48 | id | name | dept_id | id | name | 49 +----+------+---------+----+--------+ 50 | 1 | 张飞 | 1 | 1 | 财务部 | 51 | 2 | 关羽 | 2 | 2 | 人事部 | 52 | 3 | 刘备 | 3 | 3 | 科技部 | 53 | 4 | 赵云 | 4 | 4 | 销售部 | 54 +----+------+---------+----+--------+ 55 4 rows in set (0.00 sec) 56 57 mysql> SELECT * FROM emp inner join dept ON emp.dept_id=dept.id; 58 +----+------+---------+----+--------+ 59 | id | name | dept_id | id | name | 60 +----+------+---------+----+--------+ 61 | 1 | 张飞 | 1 | 1 | 财务部 | 62 | 2 | 关羽 | 2 | 2 | 人事部 | 63 | 3 | 刘备 | 3 | 3 | 科技部 | 64 | 4 | 赵云 | 4 | 4 | 销售部 | 65 +----+------+---------+----+--------+ 66 4 rows in set (0.00 sec) 67 68 mysql> select * from dept; 69 +----+--------+ 70 | id | name | 71 +----+--------+ 72 | 1 | 财务部 | 73 | 2 | 人事部 | 74 | 3 | 科技部 | 75 | 4 | 销售部 | 76 +----+--------+ 77 4 rows in set (0.00 sec) 78 79 mysql> insert into dept values(null,‘体育部‘); 80 Query OK, 1 row affected (0.07 sec) 81 82 mysql> select * from dept; 83 +----+--------+ 84 | id | name | 85 +----+--------+ 86 | 1 | 财务部 | 87 | 2 | 人事部 | 88 | 3 | 科技部 | 89 | 4 | 销售部 | 90 | 5 | 体育部 | 91 +----+--------+ 92 5 rows in set (0.00 sec) 93 94 mysql> SELECT * FROM dept left outer join emp on dept.id=emp.dept_id; 95 +----+--------+------+------+---------+ 96 | id | name | id | name | dept_id | 97 +----+--------+------+------+---------+ 98 | 1 | 财务部 | 1 | 张飞 | 1 | 99 | 2 | 人事部 | 2 | 关羽 | 2 | 100 | 3 | 科技部 | 3 | 刘备 | 3 | 101 | 4 | 销售部 | 4 | 赵云 | 4 | 102 | 5 | 体育部 | NULL | NULL | NULL | 103 +----+--------+------+------+---------+ 104 5 rows in set (0.00 sec) 105 106 mysql> SELECT * FROM dept right outer join emp on dept.id=emp.dept_id; 107 +------+--------+----+------+---------+ 108 | id | name | id | name | dept_id | 109 +------+--------+----+------+---------+ 110 | 1 | 财务部 | 1 | 张飞 | 1 | 111 | 2 | 人事部 | 2 | 关羽 | 2 | 112 | 3 | 科技部 | 3 | 刘备 | 3 | 113 | 4 | 销售部 | 4 | 赵云 | 4 | 114 +------+--------+----+------+---------+ 115 4 rows in set (0.00 sec) 116 mysql> SELECT * FROM dept left outer join emp on dept.id=emp.dept_id 117 -> UNION 118 -> SELECT * FROM dept right outer join emp on dept.id=emp.dept_id; 119 +------+--------+------+------+---------+ 120 | id | name | id | name | dept_id | 121 +------+--------+------+------+---------+ 122 | 1 | 财务部 | 1 | 张飞 | 1 | 123 | 2 | 人事部 | 2 | 关羽 | 2 | 124 | 3 | 科技部 | 3 | 刘备 | 3 | 125 | 4 | 销售部 | 4 | 赵云 | 4 | 126 | 5 | 体育部 | NULL | NULL | NULL | 127 +------+--------+------+------+---------+ 128 5 rows in set (0.00 sec)
数据库的恢复和备份
1 --备份 2 CMD窗口下:mysqldump -u root -p mydb1>d:/1.sql 3 --恢复 ,需要先创建数据库,如下面的mydb1,创建完成后才能恢复 4 CMD窗口下:mysql -u root -p mydb1 < d:/1.sql 5 --或者在mysql客户端中使用命令 6 source d:/1.sql
备份后的文件打开后,发现里面将数据都拿出来了,如果需要恢复只需要将脚本执行一遍就能将数据恢复,但是在恢复前需要在数据中新建一个数据库。
1 -- MySQL dump 10.13 Distrib 5.5.27, for Win64 (x86) 2 -- 3 -- Host: localhost Database: mydb1 4 -- ------------------------------------------------------ 5 -- Server version 5.5.27 6 7 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 8 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 9 /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 10 /*!40101 SET NAMES utf8 */; 11 /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; 12 /*!40103 SET TIME_ZONE=‘+00:00‘ */; 13 /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 14 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 15 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; 16 /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 17 18 -- 19 -- Table structure for table `employee` 20 -- 21 22 DROP TABLE IF EXISTS `employee`; 23 /*!40101 SET @saved_cs_client = @@character_set_client */; 24 /*!40101 SET character_set_client = utf8 */; 25 CREATE TABLE `employee` ( 26 `id` int(11) NOT NULL AUTO_INCREMENT, 27 `name` varchar(10) DEFAULT NULL, 28 `gender` char(2) DEFAULT NULL, 29 `birthday` date DEFAULT NULL, 30 `entry_date` date DEFAULT NULL, 31 `job` varchar(20) DEFAULT NULL, 32 `salary` double DEFAULT NULL, 33 `resume` text, 34 PRIMARY KEY (`id`) 35 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; 36 /*!40101 SET character_set_client = @saved_cs_client */; 37 38 -- 39 -- Dumping data for table `employee` 40 -- 41 42 LOCK TABLES `employee` WRITE; 43 /*!40000 ALTER TABLE `employee` DISABLE KEYS */; 44 INSERT INTO `employee` VALUES (1,‘zhangsan‘,‘M‘,‘1999-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘are you ok?‘),(2,‘lisi‘,‘M‘,‘1998-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘hello‘),(3,‘wangwu‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘buddy‘),(4,‘zhangsan‘,‘M‘,‘1999-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘are you ok?‘),(5,‘lisi‘,‘M‘,‘1998-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘hello‘),(6,‘wangwu‘,‘M‘,‘1990-01-02‘,‘2000-01-01‘,‘dance‘,15000,‘buddy‘); 45 /*!40000 ALTER TABLE `employee` ENABLE KEYS */; 46 UNLOCK TABLES; 47 48 -- 49 -- Table structure for table `exam` 50 -- 51 52 DROP TABLE IF EXISTS `exam`; 53 /*!40101 SET @saved_cs_client = @@character_set_client */; 54 /*!40101 SET character_set_client = utf8 */; 55 CREATE TABLE `exam` ( 56 `id` int(11) NOT NULL AUTO_INCREMENT, 57 `name` varchar(20) NOT NULL, 58 `chinese` double DEFAULT NULL, 59 `math` double DEFAULT NULL, 60 `english` double DEFAULT NULL, 61 PRIMARY KEY (`id`) 62 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 63 /*!40101 SET character_set_client = @saved_cs_client */; 64 65 -- 66 -- Dumping data for table `exam` 67 -- 68 69 LOCK TABLES `exam` WRITE; 70 /*!40000 ALTER TABLE `exam` DISABLE KEYS */; 71 INSERT INTO `exam` VALUES (1,‘关羽‘,85,76,70),(2,‘张飞‘,70,75,70),(3,‘赵云‘,90,65,95),(4,‘王五‘,66,NULL,NULL); 72 /*!40000 ALTER TABLE `exam` ENABLE KEYS */; 73 UNLOCK TABLES; 74 75 -- 76 -- Table structure for table `orders` 77 -- 78 79 DROP TABLE IF EXISTS `orders`; 80 /*!40101 SET @saved_cs_client = @@character_set_client */; 81 /*!40101 SET character_set_client = utf8 */; 82 CREATE TABLE `orders` ( 83 `id` int(11) DEFAULT NULL, 84 `product` varchar(20) DEFAULT NULL, 85 `price` float DEFAULT NULL 86 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 87 /*!40101 SET character_set_client = @saved_cs_client */; 88 89 -- 90 -- Dumping data for table `orders` 91 -- 92 93 LOCK TABLES `orders` WRITE; 94 /*!40000 ALTER TABLE `orders` DISABLE KEYS */; 95 INSERT INTO `orders` VALUES (1,‘电视‘,900),(2,‘洗衣机‘,100),(3,‘洗衣粉‘,90),(4,‘桔子‘,9),(5,‘洗衣粉‘,90); 96 /*!40000 ALTER TABLE `orders` ENABLE KEYS */; 97 UNLOCK TABLES; 98 /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; 99 100 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 101 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 102 /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 103 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 104 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 105 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 106 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 107 108 -- Dump completed on 2019-09-03 15:54:36
以上是mysql数据的入门知识,记录一下以后参考。
参考博文:
(1)https://www.cnblogs.com/geaozhang/p/6724393.html?utm_source=itdadao&utm_medium=referral 字符集和校对
(2)https://www.cnblogs.com/kawashibara/p/8961646.html DDL DML DCL
(3)https://www.cnblogs.com/youjianjiangnan/p/9114791.html 查看数据库和表的字符集
以上是关于MySQL入门的主要内容,如果未能解决你的问题,请参考以下文章