首页私人日誌mysql 数据库笔记

mysql 数据库笔记

admin 02-21 23:09 1031次浏览

从最开始的创建数据库,创建表,创建列开始写起,再到常用的EXISTS函数,SELECT 复杂查询,模糊查询LIKE,创建视图 等深入学习。
为了对单词加深印象,全部在DOS下演示!

创建数据库、表

create database hncu character set utf8;   

创建名为hncu编码为utf-8的数据库。

use hncu;   

 打开hncu这个数据库。(必须要打开一个数据库才能在这个数据库下面创建table哦)

创建表格stud

 create table stud(  

 sno varchar(15) not null primary key,  

 sname varchar(15) not null,  

 age int,  

 saddress varchar(15)  

 );  

 

表格添加数据:

 

insert into stud values('1001','Jack',20,'纽约');  

insert into stud values('1002','Tom',30,'纽约');  

insert into stud values('1003','张三',24,'湖南益阳');  

insert into stud values('1004','张四',15,'湖南长沙');  

insert into stud values('1005','李四',22,'湖南益阳');  

insert into stud values('1006','张三丰',80,'武侠');  

insert into stud values('1007','郭襄',75,'武侠');  

insert into stud values('1008','灭绝师太',10,'武侠');  

查看stud表的数据:

select * from stud;  

给列名取别名显示:

select sno as 编号,sname as 姓名,age as 年龄, saddress as 地址 from stud;  

select 复杂查询:

查询stud表格中age大于等于24的:

select * from stud where age>=24;  

查询stud表格中age大于等于20且下雨等于30的数据:

select * from stud where age>=20 and age <=30;  

还有一种方法:

select * from stud where age between 20 and 30;  

查询年龄等于20或者年龄等于30的stud表中的数据:

select * from stud where age=20 or 30;

还有一种方法:用 in();

查询年龄为20,22和30的stud表中的数据:

select * from stud where age in(20,22,30);  

有一个和in相对的:not in

select * from stud where age not in(20,22,30);  

模糊查询LIKE  '%'匹配所有  '_'匹配单字符  ---必须和LIKE共同使用:

也就是说通配符只能在有like的情况下使用,如果是和=一起使用,那就只是普通的字符了。

查询名字是张开头的:

select * from stud where sname like '张%';

查询名字张开头的,而且名字只有2个字符的:

select * from stud where sname like '张_';  

查询名字张开头的,而且名字只有3个字符的:

select * from stud where sname like '张__';  


查询名字中带有‘三’的:

select * fom stud where sname like '%三%';  

查询名字中带有‘三’的而且年龄大于30的:

select * from stud where sname like '%三%' and age >30;  


为表格增加一列:

alter table stud add column sex char(1);  

省略column 也可以添加

alter table stud add sex char(1);  

从stud表格删除sex列

alter table stud drop sex;  

也可以用:

alter table stud drop column sex;  

判断NULL值时,不能用‘=’号判断,而是用is:

险插入一行数据,让他的age为null;

update stud set age=20 where age=null;  

这一句是不起作用的,因为这个无法用来判断age是否为null。

应该用下面这句:

select stud set age=20 where age is null;  

作用是:如果stud表格中哪行的age为null,就设置age为20.

如果是判断哪个为空字符,就直接可以用=''  来判断。

例:

select * from stud where saddress='';  

作用是:如果stud表中有saddress为空(注意!是空,不是null),就查询显示出来。

将saddress为纽约的改为硅谷

update stud set saddress='硅谷' where saddress='纽约';   

注意:不是:这里不能写成 update table stud set...;


同时修改多个字段的值:
update stud set sname='ROSE', saddress='北京' where sno='1002';  

删除名字是悟空的行:

delete from stud where sname='悟空';  

 

知识点:

select 字段 from 表名 where 条件 and 条件 or 条件

update tableName set 需要设置的值 where 条件

delete from tableName where 条件

创建视图:cerate view 视图名 as select 子句

(虚表)---只存在内存中

create view aview as select * from stud where age>20;

从视图aview中查询年龄小于40的sname,age,ano:

select sname,sno,age from aview where age<40;  

聚合函数:

统计非null数据的行数:(*号和1 代表只要表中一行有非null的列数据,这一行就是非null)

一般要专门给个别用: as 别名

select count(*) from stud;  

select count(1) from stud;  

统计age不为空的行数:

也就是age为null就不会被统计进去。

select count(age) from stud;  

显示出stud表中所有age的平均值:

select avg(age) as averageAge from stud;  

显示所有age平均值的四舍五入。

select round(avg(age)) as averageAge2 from stud;  

还有:

Sum求和。
Max求最大值,
Min求最小值。

select  sum(age)  as sunAge from stud;  

select max(age) as maxAge from stud;  

select min(age) as minAge from stud;  


选择年龄最小的那个人的名字和年龄:

select sname , age from stud where age = ( selectt min(age) from stud );  

这样用in也可以:

select sname ,age from stud where age in(select min(age) from stud);  

再创建一个年龄等于10的行:

insert into stud value('1009','李白',10,'湖南');  

再查年龄最小的那个人的年龄:

select age from stud where age=(select min(age) from stud);  

我们可以看到,因为有2个数据的年龄都是最小值,所有显示了2行,但是它们是重复的,完全没必要显示2行。

这个时候我们就要用到:distinct ,把完全相同的行,合并显示!

select distinct age from stud where age = (select min(age) from stud);  


排序-升序和降序:

按年龄升序排:

select * from stud order by age asc;  

按年龄降序排:

select sno,sname,age from stud order by age desc;  

exists存在判断

select sname,age from stud where exists (select * from stud where age = 20);  

exists (select * from stud where age=20) ---只要存在age=20的,就返回true、

也就是exists(...) 是判断括号内的表达式是不是null的,如果是null则返回false,否则返回true;

此句因为stud存在age=20的行,所以会输出所有的sname,age。

分组  group by 

select saddress , avg(age) as 平均年龄 from stud group by saddress;  

按照saddress来分组,求出每组的平均年龄。

只要saddress不同就是不同的组!

按照saddress分组后每组的年龄总和:

select saddress,sum(age) as 年龄总和 from stud group by saddress;

有2个固定搭配:

排序:

select ... from ... where ... order by ...  

分组:

select ... from ... group by ... by ... having ... (条件判断在having后面,不是用where)

这里的sum(age)也可以用as 别名  取一个别用,在判断的时候直接可以用别名的。

字符串处理函数

<pre name="code" class="sql">Length(str) - 求字符串长度  

Ltrim(str) - 去掉左边的空格  

Rtrim(str) - 去掉右边的空格  

trim(str)  - 去掉两边的空格  

Left(str,n); - 从左边取出n个字符  

Right(str,n); - 从右边取出n个字符  

Substring(str,begin,end) -返回子串  

Reverse(str) –返回颠倒的字符串  

Lower(str) - 转成小写  

Upper(str) - 转成大写  

Concat(Str,str…..)串联字符串。  

Instr(str,s) – 返回s在str中出面的位置,没有则返回0  

这里就只选取几个来演示了:

演示left();

显示saddress开始2个字符为湖南的行

  1. select * from stud where left(saddress,2)='湖南';  

串联字符串:

select concat(snon,sname,saddress) as 串联字符串 from stud;  

instr(str,s) 返回s在str中出面的位置,没有则返回0

其实就是返回字串自一次出现的位置(从1开始计数)

select sname,instr(sname,'三') as ind from stud;'

mysql产生随机数小结 php mysql 学习笔记