时间:2021-07-01 10:21:17 帮助过:28人阅读
数据库语言 数据库的简易流程(数据库客户端软件和数据库服务软件的执行流程) 主键的概念 如何创建主键 如何创建外键 主外键关系的概念以及使用 数据库的主要类型 数据库的主要数据类型 使用SQL语句来创建数据库和表 约束分类 top的使用 Distinct的使用(去除
ALTER TABLE [dbo].[OperateType]
ADD CONSTRAINT [PK_OperateType]
PRIMARY KEY CLUSTERED ([Id] ASC);
GOALTER TABLE [dbo].[RoleAction]
ADD CONSTRAINT [FK_RoleAction_Role]
FOREIGN KEY ([Role_RoleId])
REFERENCES [dbo].[RoleInfo]
([RoleId])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GOcreate database MySchool
on
(
--括号一定是圆括号
name='MySchool_data',--数据库名称
filename='d:\MySchool_data.mdf',--物理文件名
size=5mb,--初始大小
maxsize=10mb,--最大大小
filegrowth=15% --主文件增长率
)
log on
(
name='MySchool_log',--日志文件名
filename='d:\MySchool_log.ldf',--日志物理文件名
maxsize=4mb,--最大大小
size=2mb,
filegrowth=1mb
)
gocreate table Student
(--创建学生信息表
sId int identity(1,1) primary key,--自动编号
sClassId int not null, --班级外键
sName nvarchar(50) not null,
sAge int not null,
sNo numeric(18,0),--身份证号,十八位数字,小数为
sSex char(2) not null,
sEmail varchar(50)
)alter table Score
add constraint PK_Score primary key(sId)alter table student
add constraint UQ_student unique(sNo)alter table student
add constraint DF_student default('男') for sSexalter table student
add constraint CK_student check(sAge >=18 and sAge <=100)alter table Employees
add constraint FK_DeptId_DepId foreign key(DeptId) references Department(DepId)select top 2 * from teacher
select top 20 percent * from teacher--按百分比算行数,如果出现小数,则+1(类似天花板函数celling)
select distinct(age) from teacher --求出不重复的age
select distinct age,salary from teacher --求出不重复的age和salary的组合(相当于将两个列的值加在一起的值不重复)
insert into Score(studentId,english,math)
select 1,80,100 union
select 1,80,100 union
select 3,50,59 union all
select 4,66,89 union
select 5,59,100void BuildTree(TreeNode node, List<Category> list)
{
//获得父元素的ID
int pId = Convert.ToInt32(node.Tag);
foreach (Category model in list)
{
//找出集合中 父ID 和传入参数一致的
if (model.TParentId == pId)
{
//创建子节点
TreeNode sonNode = new TreeNode(model.TName);
//将子节点自己的ID存入 节点的 TAG中
sonNode.Tag = model.TId;
//将子节点 添加到父节点中
node.Nodes.Add(sonNode);
//迭代 调用(看看子节点还有没有子节点)
BuildTree(sonNode, list);
}
}
}
public Person GetSingleInstance()
{
//在多线程情况下,需要 加锁
//这里:第一,直接锁当前的对象; 第二,定义一个线程 锁的标识volatile,然后锁住标识
lock(_mySinglePerson)
{
if(_mySinglePerson == null || _mySinglePerson.IsDisPosed)
{
_mySinglePerson = new Person();
}
}
return _mySinglePerson;
}
DataTable dt = new DataTable(); foreach(DataRow row in dt.Rows) { string str = row["columnName"].ToString(); .... }
//SQL命令生成助手(适配器) SqlCommandBuilder scb=new SqlCommandBuilder(da); //适配器在助手生成的SQL语句帮助下,成功的修改类数据库 da.Update(ds,"tempTable"); MessageBox.Show("修改成功")
提取查询语句,封装SqlHelper类(不仅要会,而且要理解思想)
select pId,
case pTypeId
when 1 then 'a'
when 2 then 'b'
else 'c'
end,
pName from PhoneNumselect studentId,成绩=(
case
when english between 90 and 100 then 'A'
when english between 80 and 89 then 'B'
when english between 70 and 79 then 'C'
when english between 60 and 69 then 'D'
when english < 60 then 'E'
else '缺考'
end
)
from scoreif exists(select * from tbl where ptId=1)
select 1
else
select 2select * from(
select row_number() over(order by ar_id)as num,*from area
)as t where runm between 10 and 14select * from PhoneNum n full join PhoneType t on n.pTypeId = t.ptId
declare @err int
set @err = 0
begin transaction
begin
print '开始事务'
update bank set balance=balance-1000 where cid='0001'
set @err=@err+@@ERROR
update bank set balance=balance + 1000 where cid='0002'
set @err=@err+@@ERROR
if(@err>0)
begin
rollback transaction
print '回滚事务'
end
else
begin
commit transaction
print '提交事务'
end
endcreate procedure usp_transferMoney
@intPerson varchar(20)
@outPerson varchar(20) '123' --可以给默认值,当参数有默认值的时候,执行的时候可以不传该参数
@abcPerson varchar(20)
as
select @intPerson,@outPerson,@abcPerson--登陆成功返回用户名不存在返回密码错误返回登陆超过次返回试登陆次数超过次返回
--drop proc login
create proc Login
@result int output,
@times int output,
@name varchar(10),
@pwd varchar(10)
as
select @times = uTimes from [user] where uName=@name
if(@times>=3)
begin
set @result = 4
return
end
if exists(select * from [user] where uName=@name)
begin
if exists(select * from [user] where uName=@name and uPwd=@pwd)
begin
set @result = 1
end
else --密码错误返回
begin
set @result = 3
update [user] set uTimes= uTimes+1 where uName=@name
select @times = uTimes from [user] where uName=@name
end
end
else --用户名不存在返回
begin
set @result = 2
end
insert into [user] values('admin','000000',0)
update [user] set uTimes=0
select * from [user]
declare @times int,@r int
exec Login @r output,@times output ,'admin111','admin'
print 'times'+cast(@times as varchar)
print 'result'+ cast(@r as varchar)EXEC usp_upGrade2 --都用默认值
EXEC usp_upGrade2 1 --第一个用默认值
EXEC usp_upGrade2 1,5 --不用默认值
针对tbL_abc表的新增之后的触发器
Create Trigger triggerName on tbL_abc
after
insert
as
begin
select * from inserted --保存了引发新增触发器的新增数据,只能在触发器中访问
end
create table #tempUsers
(
id int identity(1,1),
name varchar(20)
)create table ##tempUsers
(
id