首页 > 代码库 > T-Sql小结_02 关键字_01

T-Sql小结_02 关键字_01

SQL最重要的莫过于增删改查了,这次我们就来看下这几个的关键字

增-Create, Insert

首先创建肯定是增加吧,创建的关键字就是Create,不管是创建数据库(DB),数据表(Table),视图(View),存储过程(Procedure),函数(Function)等等,都得用到这个Create。给表里插入数据就要用到Insert了,下面让我们来实践下。  

技术分享
 1 Create Database Study_DB
 2 on  primary                  -- 默认就属于primary文件组,可省略
 3 (
 4 /*--数据文件的具体描述--*/
 5     name=Study_DB_data,  -- 主数据文件的逻辑名称
 6     filename=D:\Study_DB_data.mdf, -- 主数据文件的物理名称
 7     size=5mb,               --主数据文件的初始大小
 8     maxsize=100mb,    -- 主数据文件增长的最大值
 9     filegrowth=15%      --主数据文件的增长率
10 )
11 log on
12 (
13 /*--日志文件的具体描述,各参数含义同上--*/
14     name=Study_DB_log,
15     filename=D:\Study_DB _log.ldf,
16     size=2mb,
17     filegrowth=1mb
18 )
Create Study_DB
技术分享
 1 USE [Study_DB]
 2 GO
 3 
 4 /****** Object:  Table [dbo].[t_Grade]    Script Date: 2017/5/9 5:44:01 下午 ******/
 5 SET ANSI_NULLS ON
 6 GO
 7 
 8 SET QUOTED_IDENTIFIER ON
 9 GO
10 
11 CREATE TABLE [dbo].[t_Grade](
12     [SdId] [uniqueidentifier] NOT NULL,
13     [SdName] [nchar](20) NULL,
14     [ClassId] [nchar](10) NULL,
15     [KmId] [nchar](10) NULL,
16     [Grade] [int] NULL,
17  CONSTRAINT [PK_t_Grade] PRIMARY KEY CLUSTERED 
18 (
19     [SdId] ASC
20 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
21 ) ON [PRIMARY]
22 
23 GO
Create Table
技术分享
 1 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小明,301,001,100);
 2 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小明,301,002,79);
 3 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小明,301,003,68);
 4 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小刘,301,001,123);
 5 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小刘,301,002,135);
 6 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小刘,301,003,142);
 7 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小刚,301,001,149);
 8 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小刚,301,002,150);
 9 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小刚,301,003,150);
10 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小英,301,001,140);
11 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小英,301,002,31);
12 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小英,301,003,99);
13 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小爱,302,001,111);
14 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小爱,302,002,67);
15 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小爱,302,003,13);
16 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小胡,302,001,45);
17 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小胡,302,002,98);
18 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小胡,302,003,150);
19 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小玲,302,001,127);
20 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小玲,302,002,140);
21 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小玲,302,003,143);
22 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小松,302,001,143);
23 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小松,302,002,150);
24 Insert Into [dbo].[t_Grade](SdId,SdName,ClassId,KmId,Grade) Values(NEWID(),N小松,302,003,136);
Insert Into

建好后差不多是这个样子:

技术分享

上面的数据不太方便查看,那么我们可以定义一个视图,组合成我们想要看的样子。

技术分享
 1 Create View [V_Grade] As
 2 Select sdName, 
 3 sum(case when KmId=001 then Grade end) as Chinese,
 4 sum(case when KmId=002 then Grade end) as Math,
 5 sum(case when KmId=003 then Grade end) as English,
 6 (sum(case when KmId=001 then Grade end) +
 7 sum(case when KmId=002 then Grade end) +
 8 sum(case when KmId=003 then Grade end) )as Total
 9 from t_grade
10 GROUP BY SdName
Create View

然后再通过视图查看如下:

技术分享

这样就比较清晰一点了。

 

T-Sql小结_02 关键字_01