数据库增删改查

数据库: 三个层次:文件--服务--界面  (DBMS) 两种登录方式的设置:Windows身份登录;SqlServer身份登录。 如何设置SQLServer身份验证? 1.对象资源管理器右击--属性--安全性--SqlServer和Windows身份登录。 2.对象资源管理器--安全性--登录--sa--右击--属性--常规--设置密码 3.对象资源管理器--安全性--登录--sa--右击--属性--状态--授予,启用 重启数据库服务。

如何新建数据库? ……

.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.列

左联  右连  left join  right join   左边不丢失数据 右边不丢失数据  不加可能丢失数据

全连  full join 所有数据全出 有可能多了
....
where 查询条件

select Car.Name,Brand.Brand_Name,Productor.Prod_Name,Car.Price 
from Car
join brand on Car.Brand = Brand.Brand_Code
join Productor on Brand.Prod_Code = Productor.Prod_Code
where Price > 50
复制代码
原文地址:https://www.cnblogs.com/zdc1994/p/4233692.html