SQL初学者
SQLserver建立数据库
自定义数据库代码块
create database student on primary(
name=‘student’,–逻辑文件名称
filename=‘D:\数库学习\笔记\student_data.mdf’,–物理文讲及储存位置
size=5MB,–文件大小
filegrowth=1MB–增长方式
)
log on
(name=‘student_log’,
filename=‘D:\数库学习\笔记\student_data.ldf’,
size=1MB,
filegrowth=10%
)
- [ ] 建立默认数据库
create database stubase--建立默认数据库
- [ ] 建立表格
create table stu1(--建表
id int primary key,--学号
name varchar(20),--姓名
fs float,--分数
age int,--年龄
s_data datetime--时间
)
删除表格
drop table stu1;--删除表格
插入数据
> 方式一
insert into stu1 values(1,'小一',65,16,2015-2-6),(2,'小二',69,18,2015-2-6),
(3,'小三',98,19,2016-2-5),(4,'小四',56,20,2016-2-3)
> 方式二
insert into stu1
select 5,'小五',98,20,2015-6-6union
select 6,'小六',98,20,2015-6-7union
select 7,'小七',98,20,2015-6-8
> --复制表
select*into stu1 from stu2
## > --去重
select distinct(id)from stu1
## > 函数
select *from stu1 order by fs asc;--升序
select *from stu1 order by fs desc;--倒叙
select sum(fs)'总分数' from stu1 ;
select avg(fs)'平均值' from stu1 ;
select max(fs)'最大值' from stu1 ;
select min(fs)'最小值' from stu1;
select count(*)from stu1;--计算条数
select sum(fs)/count(*)from stu1;
select id from t3 group by id;--显示ID数
select id ,count (*)from t3 group by id;--显示学生有几行数据
select id ,count (*)from t3 group by id having ID>3;--id大于三的数
- 例题
/*-------------------------------*-------------------------------------------
| create table super_shop_info --超市商品表 |
| ( |
| s_id int primary key identity(1001,1), --主键 |
| s_name varchar(50),--商品名称: |
| s_class varchar(50),--商品类别: |
| s_count int,--商品数量: |
| s_price float--商品价格: |
| ) |
| select*from super_shop_info; |
| insert into super_shop_info values |
| ('旺旺雪饼','食品',100,20),('方便面','食品',500,3.5), |
| ('多味花生','食品',10000,2),('包菜','蔬菜',200,5), |
| ('西瓜皮','蔬菜',12,10),('冰箱','家电',55,4500.01), |
| ('空调','家电',17,11300) |
| |
| --1、每个类别分别有多少种物品 |
| select s_class ,count(*)from super_shop_info group by s_class |
| -- 2、每个类别最高价的物品是多少钱 |
| select s_class,max(s_count)'最高价' from super_shop_info group by s_class;|
| --3、按单价进行降序排序 |
| select *from super_shop_info order by s_price desc |
| --4、计算每种商品的总价值 |
| select s_name,SUM(s_count * s_price)'总价值' from super_shop_info |
| group by s_name; |
------------------------------------*----------------------------------------*/
2./----------------------------------------------------------------------------------------------------------------------------------
| 学生表stu_info(学号:id,姓名:name,性别:sex,年龄:age,课程编号:sdept)
| 课程表kc_info(课程编号:sdept,课程名:name,学号:kid)
| 分数表scort_info(课程号:sdept,学号:id,分数:scort)
|需求:创建这些表及相应约束,插入足够数据(学生表,最少10条;课程表:4条;分数表:40条
-------------------------------------------*----------------------------------------------------------------------------------------- */
create table stu_info(--学生表stu_info /学生表,最少10条;
id int primary key,
name varchar(20),
sex varchar(20),
age int,
sdept int --课程编号:sdept
)
create table kc_info(--课程表kc_info/课程表:4条;
sdept int primary key,
name varchar(20),
kid int null
)
``
–1.查询全体学生的详细记录
select *from stu_info
–2.显示前5条记录
select top 5 *from stu_info
–3.显示前50%记录
select top 50 percent *from stu_info
–4.查询所有年龄在17岁以下的学生姓名及其年龄
select name ,age from stu_info where age<17
–5.某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩
select id, sdept from scort_info where scort is null or scort=0;
–6.查所有在成绩的学生学号和课程号。
select id ,sdept from scort_info
–7.查询学生的所有信息、按学号降序排序。 ///
select *from stu_info order by id desc
–8.查询选修了课程的学生学号。
select id=‘学号’ from stu_info
–9.查全体学生的姓名及其出生年份,显示两列:姓名、出生年份。
select ‘姓名’=name from stu_info
–10.查询年龄在15~17岁(包括15岁和17岁)之间的学生的姓名、年龄。
select name ,age from stu_info where age between 15 and 17
–11.查询年龄不在15~17岁之间的男生姓名、性别和年龄。
select name ,sex,age from stu_info where age not between 15 and 17 and sex!=‘女’
初入编程看到的一句话