sql 基础练习

创建表

create table username1(id int not null,name nvarchar(10) not null,age int not null,primary key(id));
insert into username1(id,name,age)values (1,N'小妹',13);
insert into username1(id,name,age)values (2,N'多少妹',33);
insert into username1(id,name,age)values (3,N'地方',54);
insert into username1(id,name,age)values (4,N'大发',18);
insert into username1(id,name,age)values (5,N'大三',21);
insert into username1(id,name,age)values (6,N'东风公司',28);
insert into username1(id,name,age)values (7,N'的身份',66);
insert into username1(id,name,age)values (8,N'吊死鬼',4);

-----------------

更新表

update user2 set age=30,name='小心'
update user2 set name=N'大帅哥'
where age=30;
update user2 set name=N'大美女'
where (age>10 and age<50) or (age=30);

---------------------

select * from username1;

select name from username1;

select name,age from username1;

select name,age from username1 where age>20;

select name as N'姓名',age as N'年龄' from username1;

select name as N'姓名',age+10000 as N'年龄',getdate()as N'当前时间',newid() as N'编号'from username1;

select count(*)as N'几天记录'from username1;

select max(age)as N'最大年龄' from username1;

select min(age)as N'最小年龄' from username1;

select avg(age)as N'平均年龄' from username1;

select sum(age)as N'所有年龄的和' from username1;

select * from username1
order by age;

select * from username1
order by age  asc;

select * from username1
order by age  desc;

select * from username1
order by age  desc,id asc;

select * from username1
where age>20
order by age desc;

select * from username1
where name like '%妹%';

select * from username1
where name like '大%';

select * from username1
where name like'_妹n';

select null+'123' as N'null表示不知道,不表示没有'

select * from username1
where name is null;

select * from username1
where name is not null;

select * from username1
where age in(13,33,4);

select * from username1
where age between 20 and 40;

select * from username1
where age>20and age<50;

select age,count(*) from username1
group by age;

select age,count(*) from username1
group by age
having count(*)>0;//having是对分组后信息的过滤,能用的列和select中能用的列一样,having无法代替where

select age,count(*) from username1
where count(*)>0  //错误:where是对原始数据进行过滤的,聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中
group by age;

select top 3 * from username1
order by age desc;

select top 3 * from username1
where id not in(select top 5 id from username1 order by age desc)
order by age desc;

update username1 set age=13
where id=2;

select distinct age from username1;//消除重复的数据,消除完全重复的行 distanct
select name from username1
union all                   //你除非是想去除重复的数据,否则不要省略all
select name from username1
select '最大年龄是',max(age) from username1
union all
select '最小年龄是',min(age)from username1
select name ,age from username1
union all
select '总年龄是',sum(age) from username1;

select name from username1
where name like '%n%';

select abs(-444443333);//求绝对值

select ceiling(3.12);//舍入到最大数值

select floor(3.88); //舍入到最小数值

select round(3.15121435435,0); //四舍五入,后面那个参数代表精度

delete from username1
where age=13;

insert into username1(id,name,age) values(9,N'梵蒂冈',13)
select * from username1
update username1 set name=N'你好'
where age=13

-----------------------------

删除表

drop table username1

------------------

删除表中的数据

delete from user1
delete from user2 where age>20

----------------------

向表中插入数据

create table user1(name nvarchar(max) not null,age int not null,likes nvarchar(max) null)
insert into user1(name,age,likes)values('狗蛋子',18,'乒乓球')

------------------------

 

posted @ 2012-03-13 09:40  刘娇贤  阅读(344)  评论(0编辑  收藏  举报