sql server 数据库开发
1.含义:数据库设计实际上就是规划和结构化数据库中的数据对象以及这些数据对象之间关系的过程。
E-R图组成包括:
矩形表示实体集
椭圆表示属性
菱形表示关系
直线用来连接实体集与属性,同时也用来连接实体集与关系
直线上的箭头用来表示实体集之间的映射基数
2.变量
(1)局部变量
语法:declare @变量名 变量类型[,@变量名 变量类型]
例如:declare @id char(10)
--set @id = '10010001'
select @id = '10010001'
(2)全局变量
---必须以@@开头
(3)IF-ELSE
--举例:
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print 'x > y' --打印字符串'x > y'
else if @y > @z
print 'y > z'
else print 'z > y'
(4)while-continue-break
--举例:
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3
begin
print @x --打印变量x 的值
while @y < 3
begin
select @c = 100*@x + @y
print @c --打印变量c 的值
select @y = @y + 1
end
select @x = @x + 1
select @y = 1
end
(5)case
--举例:
use pangu
update employee
set e_wage =
case
when job_level = ’1’ then e_wage*1.08
when job_level = ’2’ then e_wage*1.07
when job_level = ’3’ then e_wage*1.06
else e_wage*1.05
end
3.批处理
可以使不在同一批处理语句中的sql语句相互不受影响,go关键字标志性着批处理的结束
4.常见语法举例
(1)select
select *(列名) from table_name(表名) where column_name operator value
ex:(宿主)
select * from stock_information where stockid = str(nid)
stockname = 'str_name'
stockname like '% find this %'
stockname like '[a-zA-Z]%' --------- ([]指定值的范围)
stockname like '[^F-M]%' --------- (^排除指定范围)
--------- 只能在使用like关键字的where子句中使用通配符)
or stockpath = 'stock_path'
or stocknumber < 1000
and stockindex = 24
not stocksex = 'man'
stocknumber between 20 and 100
stocknumber in(10,20,30)
order by stockid desc(asc) --------- 排序,desc-降序,asc-升序
order by 1,2 --------- by列号
stockname = (select stockname from stock_information where stockid = 4)
--------- 子查询
--------- 除非能确保内层select只返回一个行的值,
--------- 否则应在外层where子句中用一个in限定符
select distinct column_name form table_name --------- distinct指定检索独有的列值,不重复
select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name
select stockname , "stocknumber" = count(*) from table_name group by stockname
--------- group by 将表按行分组,指定列中有相同的值
having count(*) = 2 --------- having选定指定的组
select *
from table1, table2
where table1.id *= table2.id -------- 左外部连接,table1中有的而table2中没有得以null表示
table1.id =* table2.id -------- 右外部连接
select stockname from table1
union [all] ----- union合并查询结果集,all-保留重复行
select stockname from table2
(2)insert
insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
value (select Stockname , Stocknumber from Stock_table2)---value为select语句
(3)update
update table_name set Stockname = "xxx" [where Stockid = 3]
Stockname = default
Stockname = null
Stocknumber = Stockname + 4
(4)delete
delete from table_name where Stockid = 3
truncate table_name ----------- 删除表中所有行,仍保持表的完整性
drop table table_name --------------- 完全删除表
常用总汇
1.开头到N条记录
Select Top N * From 表
2. N到M条记录(要有主索引ID)
Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc
3.N到结尾记录
Select Top N * From 表 Order by ID Desc
4 如何修改数据库的名称
sp_renamedb 'old_name', 'new_name'
5 获取当前数据库中的所有用户表
select Name from sysobjects where xtype='u' and status>=0
或者:
select * from information_schema.tables
6 获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')
7 查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='P'
9 查询用户创建的所有数据库
select * from master..sysdatabases D
where sid not in(select sid from master..syslogins where name='sa')
或者
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
10 查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'
11 判断一个表是否存在
if exists(select 1 from sysobjects where name='要判断的表名' and xtype='U')
print '在'
else
print '不在'
或者
if objectproperty(object_id('要判断的表名'),'isusertable') is null
print '无此表'
else
print '有此表'
12 创建一个表和两个字段,并指定其中一个字段为自增的关键字
CREATE TABLE '+ @TABLENAME + ' (tableID BigInt identity(1,1) primary key,myUserID BigInt)'