修改表/自动增长列
修改字段(重命名):alter table abc change id id1; 修改表abc的列id为id1
修改字段(数据类型/大小):alter table abc modify name varchar(10) not null; 修改表abc的列name数据类型char大小1为varchar的大小为10
- 注意:在修改时若已经有约束条件则需要添加,否则直接按照默认值修改。
删除字段:alter table abc drop age; 删除表abc的列age
在删除时需要注意以下内容:
(1)数据和结构都被删除
(2)所有正在运行的相关事物被提交
(3)所有相关索引被删除
(4)DROP TABLE 语句不能回滚
设置为自动增长列:alter table abc modify id int primary key auto_increment; 设置表abc的列id为自动增长列(注:在列名后需要跟着数据类型)
mysql> create table abc(
-> id int,
-> name char not null,
-> age int);
Query OK, 0 rows affected (0.01 sec)
mysql> desc abc;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(1) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table abc modify name varchar(10) not null;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc abc;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table abc modify id int primary key auto_increment;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc abc;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
表中数据的增删改查
增加
全列插入:
insert into abc values(1,'xiao',25);
部分列插入:
在这儿可以看到,下面的内容只需要按顺序依次对应写入即可
insert into abc (id,name,age) values(4,'hei',25);
insert into abc (name,age) values('bai',30);
insert into abc (age,name) values(40,'bai');
mysql> insert into abc values(1,'xiao',25);
Query OK, 1 row affected (0.00 sec)
mysql> insert into abc (name,age) values('bai',30);
Query OK, 1 row affected (0.00 sec)
mysql> insert into abc (id,name,age) values(4,'hei',25);
Query OK, 1 row affected (0.00 sec)
mysql> insert into abc values(5,'bai',25);
Query OK, 1 row affected (0.01 sec)
mysql> insert into abc (age,name) values(40,'bai');
Query OK, 1 row affected (0.00 sec)
mysql> insert into abc (age,name) values(25,'hei');
Query OK, 1 row affected (0.01 sec)
mysql> select * from abc;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | xiao | 25 |
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | bai | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
5 rows in set (0.00 sec)
修改
update abc set name='hei' where id+1=6; 修改表abc中id为5的name为hei
mysql> update abc set name='hei' where id+1=6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from abc;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | xiao | 25 |
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
6 rows in set (0.00 sec)
删除
delete from abc where id=1;
查询
select * from abc;
mysql> delete from abc where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from abc;
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
5 rows in set (0.00 sec)
第一部分
计算
# 以下是已有的数据哦!!!
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
数学运算符
有加减乘除(+、-、*、/)四种,这里只做其中加和除作为演示
例如~~~ select age+2 from abc; select age/5 from abc;
注意的是,每次运算显示出的结果只作为当下的计算,而并不会修改原先值
在下面的后续计算也依旧按照原值计算并输出结果
mysql> select age+2 from abc;
+-------+
| age+2 |
+-------+
| 32 |
| 27 |
| 27 |
| 42 |
| 27 |
+-------+
5 rows in set (0.00 sec)
mysql> select age/5 from abc;
+--------+
| age/5 |
+--------+
| 6.0000 |
| 5.0000 |
| 5.0000 |
| 8.0000 |
| 5.0000 |
+--------+
5 rows in set (0.00 sec)
操作符优先级
mysql> select (age+5)/10 from abc;
+------------+
| (age+5)/10 |
+------------+
| 3.5000 |
| 3.0000 |
| 3.0000 |
| 4.5000 |
| 3.0000 |
+------------+
5 rows in set (0.00 sec)
查询使用关键字
判断是否为空值:select * from abc where age is not null;
mysql> insert into abc (id,name) values(8,'xiao');
Query OK, 1 row affected (0.00 sec)
mysql> select * from abc;
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
| 8 | xiao | NULL |
+----+------+------+
6 rows in set (0.00 sec)
mysql> select * from abc where age is not null;
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
5 rows in set (0.00 sec)
一个注意点~~~null始终为null
mysql> select age+3 from abc;
+-------+
| age+3 |
+-------+
| 33 |
| 28 |
| 28 |
| 43 |
| 28 |
| NULL |
+-------+
6 rows in set (0.00 sec)
列的别名
select age as a from abc; 将表abc中的列age以别名a显示
mysql> select age as a from abc;
+------+
| a |
+------+
| 30 |
| 25 |
| 25 |
| 40 |
| 25 |
| NULL |
+------+
6 rows in set (0.00 sec)
select 25=age from abc; 将表abc中的列age和25作比较,是为1,否为0,null不变
mysql> select 25=age from abc;
+--------+
| 25=age |
+--------+
| 0 |
| 1 |
| 1 |
| 0 |
| 1 |
| NULL |
+--------+
6 rows in set (0.00 sec)
连接符
select concat (name,age) from abc;
mysql> select concat (name,age) from abc;
+-------------------+
| concat (name,age) |
+-------------------+
| bai30 |
| hei25 |
| hei25 |
| bai40 |
| hei25 |
+-------------------+
5 rows in set (0.00 sec)
数据排重
select distinct age from abc;
mysql> select distinct age from abc;
+------+
| age |
+------+
| 30 |
| 25 |
| 40 |
+------+
3 rows in set (0.00 sec)
第二部分
数据过滤
select * from abc where name='hei';
注意:where子句中,字符和日期要包含在单引号中。
mysql> select * from abc where name='hei';
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | hei | 25 |
| 5 | hei | 25 |
| 7 | hei | 25 |
+----+------+------+
3 rows in set (0.00 sec)
比较运算
= 、 > 、 >= 、 < 、 <= 、 <>(不等于) 、 between...and... 、 in 、 like 、 is null 一共十个
between...and...:在两个值之间 select * from abc where age between 30 and 60; 双闭区间
in:等于之列表中的一个 select * from abc where id in (4,7); id等于4或者7
like:模糊查询 select * from abc where name like 'hei'; 查找‘hei’
注意:也可以用百分号表示一个或者多个字符,'%'和'-'也可以同时使用
is null空值 上面有噢~~~!!!
mysql> select * from abc where age>=25;
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
5 rows in set (0.00 sec)
mysql> select * from abc where age between 30 and 60;
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 6 | bai | 40 |
+----+------+------+
2 rows in set (0.00 sec)
mysql> select * from abc where id in (4,7);
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | hei | 25 |
| 7 | hei | 25 |
+----+------+------+
2 rows in set (0.00 sec)
mysql> select * from abc where name like 'hei';
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | hei | 25 |
| 5 | hei | 25 |
| 7 | hei | 25 |
+----+------+------+
3 rows in set (0.00 sec)
mysql> select * from abc where name like 'h%';
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | hei | 25 |
| 5 | hei | 25 |
| 7 | hei | 25 |
+----+------+------+
3 rows in set (0.00 sec)
逻辑运算
and 逻辑并 :select * from abc where age>=20 and name='hei';
or 逻辑或 :select * from abc where age>25 or name='hei';
not 逻辑否
mysql> select * from abc where age>=20 and name='hei';
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | hei | 25 |
| 5 | hei | 25 |
| 7 | hei | 25 |
+----+------+------+
3 rows in set (0.00 sec)
mysql> select * from abc where age>25 or name='hei';
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | bai | 30 |
| 4 | hei | 25 |
| 5 | hei | 25 |
| 6 | bai | 40 |
| 7 | hei | 25 |
+----+------+------+
5 rows in set (0.00 sec)
优先级
1 | 算术运算符(+ - * /) |
2 | 连接符(CONCAT) |
3 | 比较符(< > =) |
4 | IS [NOT] NULL, LIKE, [NOT] IN |
5 | [NOT] BETWEEN |
6 | NOT |
7 | AND |
8 | OR |
- count(*)
- avg() 平均值
- sum() 求和
- min() 最下值
- max() 最大值
- round() (3.14,1) 保留1位小数