首页 > 代码库 > sqlserver练习
sqlserver练习
1.基本表的练习:
create table Test( name varchar(4), age int, sex varchar(2) ) alter table Test add id char(16) alter table Test alter column id varchar(16) alter table Test drop column id drop table Test select * from Test
2.记录操作DML:
create table Test( name varchar(4), age int, id char(16) ) alter table Test alter column name varchar(6) insert into Test values(‘崔哥哥‘,15,‘311409060209‘);--注意单引号 delete from Test where name=‘崔哥哥‘ update Test set age=18 where name=‘崔哥哥‘ bulk insert Test from ‘C:\Users\Administrator\Desktop\test.txt‘ with( fieldterminator=‘ ‘,rowterminator=‘\n‘ ) select name from Test where LEN(id)=7 order by age --desc asc select * from Test
3.function用法:
--declare @a int, @b int --print rand()*26+65 --select @a=cast(‘123‘ as int), @b=convert(int,‘456‘); --print @b --print ascii(‘A‘) --print char(rand()*26+65) --print substring(‘李四‘,1,1); --select * from Test --where left(name,1)=‘崔‘ or right(name,1)=‘三‘or substring(name,2,1)=‘四‘ print getdate() print dateadd(day,2,getdate()) print dateadd(minute,2,getdate()) print dateadd(second,2,getdate()) print dateadd(hour,2,getdate()) print datediff(second,getdate(),dateadd(hour,2,getdate())) print datepart(week,getdate())-datepart(week,dateadd(month,-1,getdate())) print str(1.25,3,1)+‘asd‘ --print str(参数1,参数2,参数3) --参数2表示转换后的数据的总位数,包括小数点,正负号 --参数3表示转换后的数据的小数位数
4.bulk insert用法:
bulk insert Test from ‘C:\Users\Administrator\Desktop\test.txt‘ with( fieldterminator=‘ ‘,rowterminator=‘\n‘ )
5.cursor用法:
--declare mycursor scroll cursor for select * from Test open mycursor declare @name varchar(6),@age int,@id char(16) fetch first from mycursor into @name,@age,@id print @name print @age print @id while @@FETCH_STATUS=0 begin fetch next from mycursor into @name,@age,@id --absolute 10 print @name print @age print @id end close mycursor deallocate mycursor
6.primary key,unique,not null, check用法:
alter table Test drop column name select * from Test delete from Test select * from Test alter table Test add name varchar(6) primary key alter table Test alter column id varchar(16) not null alter table Test drop column age alter table Test add age int check( age>=0 and age<=100)
7.view and index:
create view myview as select * from Test select * from myview select * from myview where age=18 create clustered index myindex on Test(age) alter index myindex on Test rebuild drop index Test.myindex
8.trigger用法:
sqlserver练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。