初识数据库2
1.使用SQL操作数据库
- 查看数据库的命令: show databases;
- 建立数据库的命令: create datebase database_name;
- 删除数据库的命令:drop datebase database_name;
- 数据库新建完成后,要告诉数据库我正在对哪个数据库进行
操作的命令是:use database_name;
2.使用SQL对数据表操作
2.1新建数据表的语法是:
CREATE TABLE t_name(
属性名 数据类型 [约束],
属性名 数据类型 [约束],
…
属性名 数据类型 [约束]
);
代码:
mysql> create table commoditytype(
-> ct_id int(11) primary key,
-> ct_name varchar(50) not null
-> )default charset=utf8;
Query OK, 0 rows affected (0.58 sec)
2.2新增表数据的语法结构是(先建立父表):
inseret into table_name [(field1,field2…fieldn)] values (value1,value2…valuen);
注意:列名要和表中的个列一一对应,顺序名称类型一个都不能错,后面的数据内容也是一样!
代码:
mysql> insert into commoditytype (ct_id,ct_name) values (1,"玩具"),(2,"文具"),(3,"书籍");
Query OK, 3 rows affected (0.23 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from commoditytype;
+-------+---------+
| ct_id | ct_name |
+-------+---------+
| 1 | 玩具 |
| 2 | 文具 |
| 3 | 书籍 |
+-------+---------+
3 rows in set (0.06 sec)
mysql> insert into commoditytype(ct_id,ct_name) values(2,"零食");
ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'
注意:主键的唯一值重复导致报错
2.3删除表记录的语法是:
delete from table_name where condition;
代码:
mysql> delete from commoditytype where ct_id=3;
Query OK, 1 row affected (0.23 sec)
注意:删除语句一定要写删除条件,否则整张表删除!!!
mysql> delete from commoditytype;
Query OK, 2 rows affected (0.08 sec)
mysql> select * from commoditytype;
Empty set (0.00 sec)
2.4修改数据语法如下:
update table_name set field1=value1,field2=value2,…fieldn=valuen where condition;
代码:
mysql> update commoditytype set ct_name="AA" where ct_id=1;
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0
注意:一定必须不用忘了条件语句,否则整张表数据将被修改成一样的!!!!
mysql> update commoditytype set ct_name="BB" ;
Query OK, 2 rows affected (0.09 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from commoditytype;
+-------+---------+
| ct_id | ct_name |
+-------+---------+
| 1 | BB |
| 2 | BB |
+-------+---------+
2 rows in set (0.00 sec)
3.数据表的单表查询
3.0 as关键字
as关键字是起名字
代码:
mysql> select 'helloworld' as `say hi`;
+------------+
| say hi |
+------------+
| helloworld |
+------------+
1 row in set (0.02 sec)
mysql> select c_name as 商品名称, c_inprice as 商品进价, c_outprice as 商 品售价, c_outprice-c_inprice as 商品利润 from commodity;
+------------------------+----------+----------+----------+
| 商品名称 | 商品进价 | 商品售价 | 商品利润 |
+------------------------+----------+----------+----------+
3.1查询语句语法
SELECT field1,field2, … ,fieldn FROM table_nam
3.2去掉重复语句语法
SELECT DISTINCT field1,field2, … FROM table_name;
代码:
mysql> select distinct c_type from commodity;
+--------+
| c_type |
+--------+
| 1 |
| 2 |
| 3 |
+--------+
3 rows in set (0.07 sec)
3.3带BETWEEN AND关键字的条件查询语句
SELECT field1 FROM table_name WHERE field2 [not] BETWEEN valuea AND valueb;
代码:
mysql> select c_id,c_name,c_inprice
-> from commodity
-> where c_inprice between 10 and 100;
注意:between and 包括数值,not between and 不包括数值
3.4带IS NULL关键字的条件查询语句
代码:
SELECT field1 FROM table_name WHERE field2 IS [not] NULL;
mysql> select c_id,c_name
-> from commodity
-> where c_outprice is null;
注意:当null参与运算时,结果也是null
3.5带IN关键字的条件查询语句
SELECT field1 FROM table_name WHERE field2 [not] IN (value1,value2…valuen);
代码:
mysql> select c_id,c_name
-> from commodity
-> where c_id in(10,20,30,40,50);
注意:in内部数值是或关系,not in数值内部是且关系
3.6带LIKE关键字的条件查询语句
LIKE要和通配符一起用,不然没有用通配符的LIKE效果等同于“=”,常用的通配符有“_”和“%”;
前者是匹配单个字符的意思,后者是匹配任意字符;比如"_a%"表示第一个字母任意,第二个字母必须是a,后面字母任意,复合的结果有ward,martin,james等。
代码:
mysql> select c_id,c_name
-> from commodity
-> where c_name like'%玩具';
3.7带ORDER BY关键字的条件语句
排序的关键字是ORDER BY,后面跟ASC(升序,可以不写默认)或者DESC逆序;具体语法如下:
SELECT field1,field2,… FROM table_name
WHERE CONDITION
ORDER BY fieldn [ASC|DESC];
代码:
mysql> select c_id,c_type,c_inprice
-> from commodity
-> where c_type=3
-> order by c_id desc;
3.8带有LIMIT关键字的条件语句
举个例子吧:
select * from table limit 2; 只输出前2行;
select * from table limit 0,5;从第0行开始,输出5行;
select * from table order by sal limit 1;按照sal列升序排序,
只输出第一行,那么就得到sal最小的那条数据!
代码:
mysql> select c_id,c_type,c_inprice
-> from commodity
-> where c_type=3
-> order by c_id desc
-> limit 0,5
-> ;
3.9带有统计函数的条件语句
函数名称 | 函数作用 |
---|---|
COUNT()函数 | 统计记录数 |
AVG()函数 | 求平均值 |
SUM()函数 | 求和 |
MAX()函数 | 求最大数 |
IN()函数 | 求最小数 |
3.9.1COUNT()函数的两种方法实现统计
count(*)使用方式,实现对表中记录进行统计,不管是否包含NULL还是NOT NULL
代码:
mysql> select count(*)
-> from commodity;
+----------+
| count(*) |
+----------+
| 59 |
+----------+
1 row in set (0.00 sec)
count(field)使用方法对指定字段进行统计,将忽略NULL值!
代码:
mysql> select count(c_outprice)
-> from commodity;
+-------------------+
| count(c_outprice) |
+-------------------+
| 56 |
+-------------------+
1 row in set (0.00 sec)
注意:如果表中无数据,count()函数返回的是0,其它函数返回null;
3.9.2GROUP BY关键字实现统计
简单的分组查询语法如下:
SELECT * FROM tabel_name GROUP BY field;
代码:
mysql> select c_type,sum(c_inprice),avg(c_inprice),max(c_outprice)
-> from commodity
-> group by c_type;
+--------+----------------+----------------+-----------------+
| c_type | sum(c_inprice) | avg(c_inprice) | max(c_outprice) |
+--------+----------------+----------------+-----------------+
| 1 | 2448 | 116.5714 | 3000 |
| 2 | 600 | 33.3333 | 460 |
| 3 | 1053 | 52.6500 | 400 |
+--------+----------------+----------------+-----------------+
3 rows in set (0.00 sec)
3.9.3HAVING关键字
- select count(*) from table where sal>50 group by type;
- select avg(c_inprice) ,c_type from commoditygroup by c_type having avg(c_inprice) > 1
这两句语句的区别是将售价大于50的产品查询出来后进行分组,和将查询出来的所有结果进行分组排序后显示售价大于50的数据;
注意:where关键字的优先级高于having关键字
代码:
mysql> select c_type,sum(c_inprice),avg(c_inprice),max(c_outprice)
-> from commodity
-> group by c_type
-> having avg(c_inprice)>100;
+--------+----------------+----------------+-----------------+
| c_type | sum(c_inprice) | avg(c_inprice) | max(c_outprice) |
+--------+----------------+----------------+-----------------+
| 1 | 2448 | 116.5714 | 3000 |
+--------+----------------+----------------+-----------------+
1 row in set (0.00 sec)