sqlserver练习

Posted handsomecui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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练习的主要内容,如果未能解决你的问题,请参考以下文章

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

csharp Epicor标准练习片段

golang 去练习片段

ktor HTTP API 练习

sqlserver练习