码迷,mamicode.com
首页 > 数据库 > 详细

数据库——基础(数据库操作,表格操作)——增加高级查询

时间:2016-01-15 23:12:38      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 

 

笔记

LAMP:Linx(操作系统)
A(阿帕奇)——网页的应用程序
M(Mysql):体积小,应用简单
P(PHP)

 

第一步:搭建网页环境——A\M\P

WAMP:用WAMP搭建环境

DW:更好的显示

数据库的基本操作:

数据库——表结构——字段(列)

每一行数据成为一条数据(记录)

特点:关系型数据库,有严格的规范

1、必须有主键:能够唯一标识一条数据的字段

2

T-SQL:通用的数据库操作语句

自增长列code(主键列) ;连接键表 最后一个字段不加 ,#注释

创建表:
create table Family
(
Code varchar(50) primary key,
Name varchar(50) not null,
Sex bit

);
create table Nation
(
Code varchar(50) primary key,
Name varchar(50)
);

create table Info
(
Code int auto_increment primary key,
Name varchar(50),
Sex bit,
Birthday date,
Height float,
Nation varchar(50) references Nation(Code) //一般不写
)
primary key:主键
not null:非空
auto_increment:自增长列,整形,自动增长不用添加
references:引用 外键关系

删除表
drop table Family

创建数据库:create database mydb

在数据库中增加一列:alter table t1 add column addr varchar(20) not null;
这条语句会向已有的表t1中加入一列addr,这一列在表的最后一列位置。

上面这个命令的意思是说添加addr列到user1这一列后面:
alter table t1 add column addr varchar(20) not null after user1;

删除数据库:drop database mydb

CRUD:增删改查
C:create 增加数据(必须要写,可以为空)
insert into nation values(‘n001‘,‘汉族‘) -普通
insert into Name values(‘‘,‘‘)自增长列不用填,空着,不用填空字符
insert into nation (Code ,Name) values(‘n002‘,‘huizu‘)-往表中添加特定列的数据

R:read 查询数据
一、查询所有数据
select * from info 查所有
select Name from info 查特定列
二、根据条件查询
select * from info where Code=‘p001‘
select * from info where Code=‘p001‘ and nation=‘n003‘ 多条件 并关系 查询
select * from info where name=‘胡军‘ or nation=‘n001‘ 多条件 或关系 查询
select * from car where price>=50 and price<=60 范围查询
select * from car where price between 50 and 60 范围查询常用
三、模糊查询
select * from car where name like ‘%奥迪%‘ %通配符 代表任意多个字符
select * from car where name like ‘_马%‘ _通配符 代表任意一个字符
四、排序
select * from car order by price (asc) 按照价格升序排列
select * from car order by price desc 按照价格降序排列
select * from car order by price desc,oil desc 按照两列进行排序-先按照价格降序排列,再按照油耗降序排列
五、统计函数(聚合函数)
select count(code) from car 查询表中有多少条数据
select max(price) from car 取价格最大值
select min(price) from car 取价格最小值
select sum(price) from car 取价格总和
select avg(price) from car 取价格平均值
六、分组查询
select brand from car group by brand having count(*) >2 查询所有系列中数量大于2的
七、分页查询
select * from car limit 5,5 跳过几条数据取几条数据
八、去重查询
select distinct brand from car 可以去哪一列的重复

 


U:update 修改数据
update nation set Name=‘huizu‘-全部修改
update nation set Name=‘huizu‘ where Code=‘n002‘-修改某一条数据
update nation set Name=‘huizu‘ where Code=‘n003‘ and Code=‘n004‘-修改某一条数据

D:delete 删除数据
delete from nation-删除整个表中的数据
delete from nation where Code=‘n001‘-删除一条数据

高级查询

一、多表连接
1.select * from Info,Nation where Info.Nation=Nation.Code
select Info.Code,Info.Name,Nation.Name from Info,Nation where Info.Nation=Nation.Code
2、join连接
select * from Info join Nation on Info.Nation=Nation.Code
二、多表联合(列的扩展)
select * from Info where Code=‘p001‘
union
select * from Info where Nation=‘n001‘
三、子查询(无关子查询)
select * from Info where Nation (!)= (select Code from Nation where Name=‘汉族‘)
select * from Info where Nation (not)in (select Code from Nation where Name=‘汉族‘ or Name=‘苗族‘)
四、子查询(相关子查询)
select * from Car a where a.Oil<(select avg(Oil) from Car b where b.brand=a.brand)
第一次查询里层查询变为 select avg(Oil) From car where brand=‘b001‘

数据库——基础(数据库操作,表格操作)——增加高级查询

标签:

原文地址:http://www.cnblogs.com/Chenshuai7/p/5134645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!