SQL server 基本语句

--查询数据库是否存在

if exists ( select * from sysdatabases where [name]='TestDB')

print 'Yes, the DB exists'

else

print 'No, need a new one?'

--新建一个数据库

create database TestDB on

(

    name = 'TestData',

    filename = 'G:DBSKeyTest.mdf',

    size = 3,

    filegrowth = 2

)

log on

(

    name = 'TestLog',

    filename = 'G:DBSKeyTest.ldf',

    size = 3,

    filegrowth = 10

)

--drop database TestDB

use TestDB

go

--新建一个表

create table [Scores]

(

    [ID] int identity(1,1) primary key,

    [Student] varchar(20) ,

    [Subject] varchar(30),

    [Score] float

)

--drop table [Scores]

--修改表中的一列

alter table Scores alter column [Student] varchar(20) not null

--新增一列

alter table Scores add Birthday datetime 

--删除一列

alter table Scores drop column Birthday

--往表中插入单条数据,方法:带列名

insert into Scores(Student,Subject,Score)

values('张三','语文','90')

--往表中插入单条数据,方法:不带列名,但要求值的类型要和列字段类型对应

insert into Scores

values('张三','英语','95')

--插入多条数据:union或者union all

insert into Scores(Student,Subject,Score)

select '李四','语文','89'

union all

select '李四','英语','78'

--删除表中数据,没有条件时,删除所有

delete from Scores where ID in(7,8)

--修改表中数据

update Scores 

set Student='王五',Score='94'

where ID=10

--查看数据

select * from Scores

--查看表中最大的identity

select @@identity

--查找两个表中列的值是否相同,相同则不显示,不相同的则查出结果

select * from B_PowerStation a where not exists(select * from B_PowerPile b where a.ID=b.ID)

--或者利用dbcc命令查看表中最大的identity

dbcc checkident('Scores',noreseed)

--创建视图,全部省略视图的属性列名,由子查询目标列的字段组成

create view StudentView

as

select Student,Subject,Score

from Scores

--加上with check option,以后对视图的操作(增,改,删,查)都会自动加上where ID>3

/*

create view StudentView

as

select Student,Subject,Score

from Scores

where ID>3

with check option

*/

--创建视图,全部定义属性列名,需要定义列名的情况:

----某个目标列(子查询)不是单纯的属性列,而是聚集函数或列表达式

----多表连接时选出了几个同名列

----需要在视图中为某个列启用新的更合适的名字

create view IS_Student(Student,Subject,MaxScore)

as

select Student,Subject,Score

from Scores

where Score=(select max(Score) from Scores)

--查询视图,和基本表完全样,只不过如果视图中有with check option,会自动加上那个条件

select * 

from StudentView

--查询自定义列名的视图

select * 

from IS_Student

--对视图的insert/delete/update,和对基本表的操作一样,并且最终都是用RDBMS自动转换为对基本表的更新

--并不是所有的视图都是可更新的,因为有些视图的更新不能有意义的转换成对相应基本表的更新

--删除视图

drop view StudentView

1.  SQL常用命令使用方法

(1)数据记录筛选

select*from Products查询出Products表里面的所有信息

select ProductID,ProductName from Products查询出Products表里面所有ProductID,ProductName

select ProductID,ProductName from Products where ProductID=1查询出Products表里ProductID=1的所有ProductIDProductName

select* from employee where fname='Paul' and job_id=5 查询出employee表中fname=Paul并且job_id=5的所有记录

select*from Products where ProductID in(4,5,6)查询出Products表中ProductID,5,6的所有信息

select*from Products where UnitPrice>10 and UnitPrice<30 order by UnitPrice查询出Products表中<UnitPrice<30的所有信息并按照UnitPrice的大小由小到大排序

select*from Products where UnitPrice between 10 and 30 order by UnitPrice上面的另外一种写法

select * from Employees where FirstName like 'A%'查询出EmployeesFirstName里面第一个字母是A的所有人信息

select*from Employees where FirstName like '%A%'查询出EmployeesFirstName里面中间有A的所有人信息

select*from Employees where FirstName like '%A'查询出EmployeesFirstName里面最后一个字母是A的所有人信息

select count(*) from Employees查询出Employees表中的所有记录数

select min(Unitprice)from Products查询出Products表中Unitprice的最小值

select max(Unitprice)from Products查询出Products表中Unitprice的最大值

select avg(Unitprice)from Products查询出Products表中Unitprice的平均值

select sum(Unitprice)from Products查询出Products表中Unitprice的总和

select * from Products where Unitprice> (select avg(Unitprice) from Products)有子查询查找出比平均值高的商品信息

select top 5* from Products查询出前五条的记录信息

select distinct [name] from Category 查出Category 中不重复的name

select count(distinct name) from Category 查出Category 中不重复的name的数量

(2) 更新数据记录

sql="update 数据表set 字段名=字段值where 条件表达式"

sql="update 数据表set 字段=,字段=……字段n=n where 条件表达式"

(3) 删除数据记录

sql="delete from 数据表where 条件表达式"

sql="delete from 数据表" (将数据表所有记录删除)

(4) 添加数据记录

sql="insert into 数据表(字段,字段,字段…) values (,,…)"

sql="insert into 目标数据表select 字段名from 源数据表" (把源数据表的记录添加到目标数据表)

(5) 数据记录统计函数

AVG(字段名得出一个表格栏平均值

COUNT(*&brvbar;字段名对数据行数的统计或对某一栏有值的数据行数统计

MAX(字段名取得一个表格栏最大的值

MIN(字段名取得一个表格栏最小的值

SUM(字段名把数据栏的值相加

引用以上函数的方法

sql="select sum(字段名) as 别名from 数据表where 条件表达式"

set rs=conn.excute(sql)

 rs("别名"获取统的计值其它函数运用同上

(6) 数据表的建立和删除

CREATE TABLE 数据表名称(字段 类型(长度),字段 类型(长度) …… )

CREATE TABLE tab01(name varchar(50),datetime default now())

DROP TABLE 数据表名称 (永久性删除一个数据表)

2. 记录集对象的方法

rs.movenext 将记录指针从当前的位置向下移一行

rs.moveprevious 将记录指针从当前的位置向上移一行

rs.movefirst 将记录指针移到数据表第一行

rs.movelast 将记录指针移到数据表最后一行

rs.absoluteposition=N 将记录指针移到数据表第N

rs.absolutepage=N 将记录指针移到第N页的第一行

rs.pagesize=N 设置每页为N条记录

rs.pagecount 根据 pagesize 的设置返回总页数

rs.recordcount 返回记录总数

rs.bof 返回记录指针是否超出数据表首端true表示是false为否

rs.eof 返回记录指针是否超出数据表末端true表示是false为否

rs.delete 删除当前记录但记录指针不会向下移动

rs.addnew 添加记录到数据表末端

rs.update 更新数据表记录

3.  排序

desc就是用于查询出结果时候对结果进行排序是降序排序asc就是升序。。要用与order by一起用 

例如select * from student order by id desc; 就是把选出的结果经过id从大到小排序把资源返回

还可以select * from student order  by age desc,id desc;“,”号隔开多个排序条件这样先按age 再按 id就是说先按age从大到小排序如果有相同年龄的那么相同年龄的学生再按他们的id从大到小排序

DESC指排序时按降序排序ASC是升序

约束篇:

1.主键约束

要对一个列加主键约束的话这列就必须要满足的条件就是分空

因为主键约束就是对一个列进行了约束约束为非空不重复

以下是代码   要对一个列加主键列名为id,表名为emp

格式为

alter table 表格名称 add constraint 约束名称 增加的约束类型 列名

例子

alter table emp add constraint ppp primary key (id)

2.check约束

就是给一列的数据进行了限制

比方说年龄列的数据都要大于的

表名emp 列名age

格式

alter table 表名称 add constraint 约束名称 增加的约束类型 列名

例子

alter table emp add constraint xxx check(age>20)

3.unique约束

这样的约束就是给列的数据追加的不重复的约束类型

格式

alter table 表名 add constraint 约束名称 约束类型列名

比方说可以给ename列加个uniqueename列的数据不重复

例子

alter table emp add constraint qwe unique(ename)

4.默认约束

意思很简单就是让此列的数据默认为一定的数据

格式

alter table 表名称 add constraint 约束名称 约束类型 默认值 for 列名

比方说emp表中的gongzi列默认为

alter table emp add constraint jfsd default 10000 for gongzi

5.外键约束

这个有点难理解了外键其实就是引用

因为主键实现了实体的完整性

外键实现了引用的完整性

应用完整性规定所引用的数据必须存在

其实就是个引用

比方说一个表名称叫dept 里面有列数据 一列是ID一列是ENAME

id表示产品的编号

ename表示产品的名称

另外一个表格名称是emp 里面有列数据一列是ID 一列是DID

id:表示用户号

did表示购买的产品号

要让emp表中的did列去引用dept表中的id

可以用下面的方法

格式

alter table 表名 add constraint 约束名称 约束类型 (列名) references 被引用的表名称列名

例子

alter table emp add constraint jfkdsj foreign key (did) references dept (id

修改约束

 

原文地址:https://www.cnblogs.com/Mzg121584668/p/7081041.html