数据库--增、删、改、查(笛卡尔积)

 

 

 

 

 

如何新如建数据库? ……

.ldf——日志文件

.mdf——主数据文件

.ndf——次数据文件 一个数据库中,可以有多个日志文件,多个次数据文件,但只能有一个主数据文件。

如何新建表? ……

行次序无关,列次序无关。

SQL语句  DDL DML(增、删、改、查) DCL

insert into 表名(列名,列名,列名,...)

values(值,值,值,....)

insert into 表名 values(值,值,值,值。。)

、简单查询

select * from 表名 select 列名,列名,...from 表名 ——投影

等值与不等值查询 select * from 表名 where 列名=值 --等值查询

不等值查询

select * from 表名 where 列名 <> 值

select * from 表名 where 列名 > 值 >=

select * from 表名 where 列名 < 值 <=

多条件查询 

逻辑与(and),逻辑或(or)

select * from 表名 where 条件1 and 条件2 ...

select * from 表名 where 条件1 or 条件2 ...

如果在where筛选条件中,既出现and又出现or,则先运算and。除非使用小括号改变优 先级。

范围查询。

select * from Car where Price >=30 and Price<=50

select * from Car where Price between 30 and 50

select * from Car where Oil=7.4 or Oil=8.5 or Oil=9.4

select * from Car where Oil in(7.4,8.5,9.4)

模糊查询。

一般不用=,而是用like

%——任意多个任意字符

_——一个任意字符

select * from Car where Name like '宝马%'

宝马%——以宝马开头的

%宝马——以宝马结尾的

%宝马%——只要含有宝马这两个字就可以。

__宝马%——代表第三个字符以宝马开头的。

去重查询:

select distinct 列名 from car ——如果列中有重复值,则只查1个出来。

取前几条数据 select top 数量 [列名|*] from 表名

排序

select * from car order by price asc  ——默认是升序 ascending descending

select * from car order by price desc

select * from Car order by Oil asc,Price desc  ——Oil主排序,Price次排序

delete from car ——删除全部数据

delete from car where 条件 ——这里的条件是跟select的条件是一样的。

update 表名 set 列名=值,列名=值..... where 条件

update Car set Price = Price + price * 0.15 where Name like '宝马%'

update Car set Name='300C 3.5L 商用车',Oil='9' where Code='c012'

分组查询:

排序

链接查询: 第一步:求笛卡尔积 select * from Info,nation

第二步:根据两个表相对应的列,对笛卡尔积进行有效数据的筛选。 select * from Info,Nation where Info.Nation = Nation.code

第三步:调整显示要查询的列 select Info.Code,Info.Name,Info.Sex,Nation.Name,Info.Birthday from Info,nation where Info.Nation=Nation.Code

select * from 表名1 --

join 表名2 on 表名1.列 = 表名2.列

join 表名3 on 表名2.列 = 表名3.列

.... where 查询条件

select Car.Name,Brand.Brand_Name,Productor.Prod_Name,Car.Pricefrom Car

join brand on Car.Brand = Brand.Brand_Code

join Productor on Brand.Prod_Code = Productor.Prod_Code where Price > 50

 

posted @ 2015-01-08 15:40  奔跑的小黄鸡  阅读(576)  评论(0编辑  收藏  举报