SQLserver初學者

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大於三的數


在這裏插入圖片描述

  1. 例題
/*-------------------------------*-------------------------------------------
|  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!=‘女’

在這裏插入圖片描述

初入編程看到的一句話

孔子說:講給我聽 我會忘記,指給我看,我會記住 讓我去做 我會理解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章