sql基础资料

SQL server 基础资料

·系统数据库基本类型

数据类型

符号标识

整数型

bigint,int,smallint,tinyint

精确数值型

decimal,numeric

浮点型

float,real

货币型

money,smallmoney

位型

Bit

字符型

char,varchar/varchar(MAX)

Unicode字符型

nchar,nvarchar/nvarchar(MAX)

文本型

text,ntext

二进制型

binary,varbinary/varbinary(MAX)

日期时间类型

datetime,smalldatetime,date,time,datetime2,datetimeoffset

时间戳型

Timestamp

图像型

Image

其他

Cursor,sql_variant,table,uniqueidentifier,xml,hierarchyid

 

 

 

·创建数据库

Create  database  TEST

On

Primary

(

         NAME=’TEST’,               --主数据库文件名

         FILENAME=’D:/program file/TEST.mdf’,             --主数据库文件存放路径

         SIZE=5MB,             --主数据库文件的初始大小

         MAXSIZE=100MB,                 --主数据库文件增长的最大值

         FILEGROWTH=2MB                            --主数据库文件的增长率

)

LOG ON

(

         NAME=’TEST_log’,                --日志文件的逻辑文件名

         FILENAME=”D:/program file/TEST_log.ldf”,               --日志文件存放路径

         SIZE=1MB,             --日志文件的初始大小

         FILEGROWTH=10%          --日志文件的增长率

)

 

·修改数据库

Alter  database  TEST

REMOVE FILE TEST_log  --删除数据库文件

 

·删除数据库

Drop  database TEST

 

·创建表

USE TEST

Go

Create table user

(

         userID               char(20)            not null             primary key ,

    Username        char(18)            not null ,

         Password         varchar(22)      not null ,

        

)

 

·修改表

Alter table user

 Add column a varchar(20) null  --在user表中添加一个新的列a

 

Alter table user

Drop column a  --删除user表中的a

 

·删除表

Drop table 表名

 

 

·插入数据记录

Insert into 表名 (列名1,列名2) Values(值1,值2)

Insert into user(userID,Username,Password)

Values(‘001’,’JC’,’0123456’)

 

·修改数据记录

Update  表名  set 列名=修改的值  where 修改的条件

Update  user set Username=’tang’ where userID=’001’  --修改userID为001的用户名为tang

 

 

·删除数据记录

Delete      from  表名   where 删除的条件

Delete  from  user where userID=’001’  --删除userID为001的记录

 

 

Truncate语句清除数据

Truncate table <表名>

注意:使用truncate  table 进行数据表的清除,只会清除对应表的所有数据,但不是删除表,且保留整个表的结构(字段),约束条件

 

·查询数据记录

Select  <字段>  from 表名 where 查询条件

 

指定条件查询

Select  *  from  user  where  userID=’001’  --查询userID为001的记录

 

模糊条件查询

Select       *  from  user  where username  like “%tang%”  --查询user表username中含tang的所有记录

 

指定查询前10条记录

Select top(10)  *  from  user  where username  like  ”%tang%”

--查询user表中username含tang的前10条记录

 

 

多表查询

 

表1 user (userID,username,password

表2 user_info (id,userID,phone,address,email)

 

方式一-(条件匹配)

Select user.userID,user.username,user.password,user_info.phone,user_info.address,user_info.email

From user ,user_info

Where user.userID=user_info.userID

 

方式二-(内连接)

Select * from user inner join user_info

On user.userID=user_info.userID

 

方式三-(左连接)

Select user.userID,username,password

From user left outer join user_info

On user.userID=user_info.userID

 

方式四-(右连接)

Select user.userID,username,password

From user right outer join user_info

on user.userID=user_info.userID

 

 

方式五- (全外连接)

Select  user.ID ,username,password

From user full outer join user_info

On user.userID=user_info.userID

 

这几种方式的区别:

内连接:内连接按照on所指定的连接条件合并两个表,返回满足条件的行。

左连接:结果表中除了包括满足连接条件的行外,还包括左表的所有行

右连接:结果表中除了包括满足连接条件的行外,还包括右表的所有行

完全外连接:结果表中除了包括满足连接条件的行外,还包括两个表的所有行

 

查询结果升序

Select *  from user order by userID ASC

 

查询结果降序

Select *  from user order by userID DESC

 

 

创建视图

Use TEST

Go

Create view SCL

As

Select user.userID,user.username,user.password,user_info.phone,user_info.address

From user inner join user_info

On user.userID=user_info.userID

查询视图

Select * from SCL

 

 

创建索引

Use TEST

GO

Create  nonclustered  index  userID  on user(userID)  --创建user表userID的非聚集索引

 

创建唯一索引

Use TEST

GO

Create unique index uname

On user(username)                  --创建user表username的唯一索引

 

 

 

 

过滤查询结果的重复值

Select  distinct address  from  user_info   --查找user_info表并过滤address中的重复值

 

 

替换查询结果中的数据

Select userID,phone,address,address_no=

Case

         When address_no= 1 then “北京”

         Wben address_no=2 then “上海”

         Else “无此地址”

End

From user_info

 

 

EXISTS子查询

Select * from user where exists(

         Select * from user_info where userID=’001’

)

EXISTS谓词用于测试子查询的结果是否为空表,若子查询的结果集不为空,则EXISTS返回true,否则,返回false

 

 

Group by分组查询

Select * from user_info

Group by county_on

 

Having子句

Having的查询条件与与where的查询条件类似,不过having支持聚合函数,而where不支持聚合函数

Select count(county_on) from user_info

Having county_on >10

 

 

 

原文地址:https://www.cnblogs.com/JcHome/p/8434989.html