数据库基础介绍

--删除列
--alter table 表名drop column 要删除的列名;
alter table tblStudent drop column tSClassId;

--添加列
--alter table 表名add 列名类型
alter table tblStudent add tSClassId int

--修改列
--alter table 表名alter column 列名类型
alter table tblStudent alter column tSClassId int not null

--添加主键约束
--alter table 表名add constraint 主键约束名primary key(主键列)
alter table TblStudent add constraint 
PK_TblStudent_tSId Primary Key(tSId)

--删除
alter table tblStudent drop constraint PK_TblStudent_tSId

select查询
use TestDataBase
--用法
select * from Student  -- select 后的*,表示所有列
--用法,只显示部分列
SELECT stuId,stuAddress,'我也不知道' as 'new Col' from Student
--用法,添加一列,列可以进行计算
select stuName,stuAddress,stuName+stuAddress as '姓名+地址' from Student
select testId+stuId from Score

列的重命名:
--重命名列名
--方法,通过as

SELECT stuId as [学号],stuName,  stuAddress from Student

--方法,空格后直接写别名
SELECT stuId as [学号],stuName 姓名,  stuAddress from Student

--方法,别名=列名

SELECT stuId as [学号],stuName 姓名, '地址'=stuAddress  from Student


Union 合并
--合并结果集union
--要求:两个结果集的列数必须相同  2两个结果集对应列的数据类型相同
select * from Score
select stuId,testBase, testName,testDate from score where stuId=1
union --合并结果集
select stuId,testBase, testName,testDate from score where stuId=5

select col1 from tblTestA    
union						--注意,union是把后面的结果集中的数据并入了第一个
							--select语句的结果集中
select Col2 as 'aaa' from tblTest2


批量插入:

--插入多条数据的方法:
--方法
--语法:insert into 表名[(列名,列名....)]
--------select 一个结果集   
--注意,结果集中的列必须与表名后的列相同且类型相同
insert into TblClass (tClassName,tClassDesc)
select '北京天安门','我喜欢的地方'
union select  '**班','明天开'
union select  '**班','都是小黑'
union select  '**','优惠班'


insert into TblClass
select tSAddress,tSPhone from TblStudent
select * from TblClass

insert into TblStudent 
select  tSGender, tSAddress, tSPhone, tSAge,
 tSBirthday, tSCardId, tSClassId from TblStudent
 select * from TblStudent



--第二种批量插入数据的方法:
--select * from 表名在列名后加上into 新表名  注意:新表必须不存在
--注意创建的新表数据和自动增涨列都过来了,主键约束没有了。
select * into newClassA from TblClass

--部分列
select tClassName,tClassDesc into newClassCols from TblClass
select * from newClasscols
select * from newClassA

--加where条件的
select tClassName,tClassDesc into newClassWhere  from TblClass 
where tClassId<=4

select * from newClassWhere

update:
--update
--语法:
--
-- update 表名set 要更新的列名=新值[,要更新的列名=新值] [where 表达式]
use TestDataBase
select * into newStudent from Student

select * from newStudent

--最基本的更新语句
update newStudent set classId=1

--一次更新多列
update newStudent set stuIsDel=1,classId=2
select * from newStudent
updaTE newStudent set classId=classId+1

select * from score
update Score set testBase=testBase-1
select * from score
--部分更新,加where条件,我们以后更用的大部分update语句一般
--情况下都应该有where条件
select * from Score where stuId=1
update Score set testBase=testBase+5 where stuId=1
select * from Score where stuId=1

delete:
--删除的语法delete from 表名
select * from newStudent 
delete from newStudent

--删除和update一样,是一个危险的操作,执行时一定要慎重.要好好想一下where条件是什么?
select * from newStudent

select * from newStudent where classId=1 and stuSex='f'
delete from newStudent where classId=1 and stuSex='f'



--第二种删除数据的方法truncate ,他可以删除表中所有的数据,
--并把表中自动增涨种子也初始化.
--语法: truncate table 表名
--注意:
--1.只能删除所有数据,不能删除部分数据
--2.删除速度比DELETE快
--3.truncate删除是破坏性,不写日志
--4..truncate语句不触发delete触发器
--5.truncate语句会把表中的自动编号重置为默认值
select * from newStudent 
truncate table newstudent


约束:
--添加一个唯一约束
--语法: alter table 表名add constraint 约束名约束的关键字(列名)...
alter table tblclass add constraint
UQ_tblClass_tClassDesc Unique(tClassDesc)

select * from TblClass
insert into TblClass(tClassDesc) values('ttt12-28开班')
--添加一个默认值约束
alter table tblClass add constraint 
DF_TblClass_tClassDesc Default(N'开班') for tClassDesc
insert into TblClass(tClassName) values('测试就业班')

--Check约束,就是写一个表达式,当数据往数据库表中插入时,执行这个表达式,
--如果表达式的值为true,则允许插入这个数据,如果表达式的值为false,则不允许
--插入这个数据

update TblClass set tclassmaxstudent=50
select * from TblClass
insert into TblClass values('11','222333322',35)

select LEN(tclassName),tclassName from TblClass

--添加一个检查tclassName字段中的数据必须大于等于的约束

alter table tblClass add constraint CK_TblClass_TClassName
Check(len(tClassName)>=3)

insert into TblClass values('11','222333322',35)


--建外键,在外键表上点右键,设计
--外键约束一旦创建好后,添加数据库,必须选添加主键表,再添加外键表
--删除数据时,一定要先删除外键表,再键除主键表中的数据
--写代码创建外键约束
alter table tblStudent add constraint
FK_TblStudent_tSclassId Foreign key(tSClassId)
References TblClass(tClassId) on delete cascade

delete from TblClass where tClassId=4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值