sql学习

create database test
use test
sp_help
sp_helpfile
sp_helpfile test
--sp_detach_db
--sp_attach_db

create table stu
(
    stu_id char(10),
    stu_name char(10),
    stu_age int
)

select * from stu
insert into stu values('1001','mjj',12)

create table stu_temp
(
    stu_id char(10),
    stu_name char(10),
    stu_age int
)


insert into stu_temp values('1003','mjj1',15)
select * from stu_temp
--insert into stu select * from stu_temp
select * from stu

insert into stu default values
delete from stu where stu_id != '1003'
insert into stu(stu_id) values('1005')

select * from stu where stu_id = '1005'
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',17)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',12)

insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',16)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',15)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',14)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',13)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',11)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',9)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',8)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',7)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',6)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',5)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',4)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',3)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',2)
insert into stu(stu_id,stu_name,stu_age) values ('1006','mjj2',1)

delete stu where stu_age between 1 and 4    
select * from stu

delete from stu where stu_id = '1005'
update stu set stu_id = '2001' where stu_age = '15'
delete stu where stu_id = '2001'
delete stu where stu_id != '2001'
delete stu

update stu set stu_name = 'mjj3' where stu_id != '2001'

create table test
(
    test_id char(13),
    test_name char(20),
)
select * from test
delete test

insert into test values('hello1','hello2')

declare @var1 char(20),@var2 char(20)
set @var1 = 'china'
set @var2 = ' is  a greate country'
select @var1,@var2

declare @str char(20)
set @str = 'welcome'
print @str
print '12' + 13
print 12+'13'

print @@version
 
select * from stu where stu_age  < 10 and stu_name = 'mjj2'

select * from stu where stu_age like '1%'

begin
    if exists(select * from stu where stu_name = 'mjj2')
    begin
        select * from stu where stu_age between 10 and 13
    end
    else
    begin
        select * from stu where stu_age between 14 and 17
    end
end

select * from stu

DECLARE @counter int,@sum int
SET @counter = 1
SET @sum = 0
WHILE @counter <= 100
BEGIN
SET @sum = @counter + @sum
SET @counter = @counter + 1
END
PRINT  @sum

declare @i int
set @i = 1
while (@i  <=  10)
begin
    if(@i = 6)
        break
    print(@i)
    set @i=@i+1
End
print(@i)

CREATE  DATABASE  ONEDB
sp_help ONEDB
use ONEDB
sp_helpfile
alter database ONEDB modify name = DatabaseTest
use DatabaseTest

create table school
(
    学号 int primary key identity ,
    名称 char(6) not null,
    专业方向 varchar(10) not null,
    系部代码 char(2) not null,
    备注 varchar(50)
)

drop  table table_name
select * from school

alter table school drop column 备注

alter table school add 特长 nchar(10)

select * from school


use test
sp_helpfile
select * from stu
select stu_id as 学号,stu_name as 学生姓名 ,stu_age as 学生年龄 from stu

select '学生信息' = stu_id + stu_name,stu_age as '学生年龄'from stu

select * from stu
insert into stu values('4001','lyf1',12)
select top 3 * from stu order by stu_id desc --asc
select top 2 percent * from stu
insert into stu  values(NULL,NULL,NULL)

delete stu where stu_id is NULL

select * from stu_temp

insert into stu select * from stu
select * from stu
delete table stu
delete stu where stu_age = 17

--字符串函数
SELECT CHARINDEX('FIT','My FIT Course' )
SELECT LEN('SQL Server课程')
SELECT LOWER('SQL Server course')
SELECT UPPER('sql server SQL Server course')
SELECT LTRIM ('  zhizhenyu ')
SELECT RTRIM (' zhizhenyu ')
SELECT RIGHT('study hard',3)
SELECT REPLACE('beautiful girl','a','s')
SELECT STUFF('ABCDEFG', 2, 3, 'my music my world')

--日期函数
SELECT GETDATE()
SELECT DATEADD(mm,4,'99/01/01')
SELECT DATEDIFF(mm,'99/01/01','99/05/01')
SELECT HOST_NAME()
SELECT DATALENGTH ('china')
SELECT CONVERT (VARCHAR (5),12345)

select * from stu
delete stu where stu_name like 'mjj%'

select * from stu where stu_name not like 'mjj%'

select stu_id as '学生学号' ,stu_name as '学生姓名',stu_age as '学生年龄'
from stu where stu_age in (12,13,17)


select sum(stu_age) from stu where stu_age > 12
select avg(stu_age) from stu where stu_age > 12
select max(stu_age) from stu where stu_age > 12
select min(stu_age) from stu where stu_age > 12
select count(stu_age) from stu where stu_age > 12

select * from stu where stu_age > 12 order by stu_age desc

select * from stu where stu_age > 12 order by stu_age asc

update stu
set stu_name = 'lyf2' where stu_name = 'lyf1'

select stu_name as '学生姓名',count(stu_age) from stu group by stu_name

--where / group by /having
 



上一篇:go language --- techory diary 15 Dec


下一篇:Git和Gitlab协同工作