-一,创建数据库 。create database XXXDB--创建数据库use XXXDB--使用数据库create table Department(--创建表nvarchar() not ,--列与列之间以逗号隔开dept_name nvarchar() not , dept_head nvarchar() not )select * from Department--查询表中的数据insert into Department values(,,)--插入数据 ,(,,)--Department where =--删除指定的列数据--创建表create table Employee( EMP_ID nvarchar() not , EMP_NAME nvarchar() not , ADDRESS nvarchar() not , DEPT_ID nvarchar() not ,--EMP_id int not )--选择使用表select * from Employee--在表中插入数据insert into Employee values(,,,) ,(,,,), (,,,), (,,,)--drop table Department--删除表--二,创建数据库实例create database schoolDB--主数据库 .mdf--日志文件 .ldf--D盘create database BankDB on primary( =, =, = , =, =)--注意:size必须是不小于5MBlog on( =, =, = , =, =%)--日志数据库 中 size没有大小要求create database testDB on primary( =, =, =, =, =)log on( =, =, =, =, =%)use testDBcreate table testTable( EmployeeID nvarchar() not , Resiednce nvarchar(), Office nvarchar(), MobileNumber nvarchar())insert into testTable values(,,,), (,,,), (,,,), (,,,)select *from testTable------------------------------------------------ --三,创建架构,创建表。select UNICODE()select CHAR()gocreate schema ONE--创建架构gocreate schema TWOgocreate table CLASS1( int primary , nvarchar())create table ONE.CLASS1( int primary , nvarchar())create table ONE.CLASS2( int primary , nvarchar())--插入数据--inserted. * deletedcreate table conInfo( int primary identity(,),--不用考虑,直接插入三个值nvarchar() ,--地址oInfo nvarchar() , mInfo nvarchar())insert into conInfo inserted. * values(,,), (,,), (,,)conInfo deleted.* where =--deleted.*必须写update conInfo =--后果严重update conInfo =where =update conInfo inserted.* =where =--语法出错update conInfo =inserted.* ,,deleted.* where =select *from conInfo where oInfo is select *from conInfo where oInfo is not select ,isnull(,),isnull(,),isnull(mInfo,) from conInfoselect ,isnull(,),isnull(,)as oInfo,isnull(mInfo,)as mInfo from conInfoselect * from conInfo四,使用子查询。use AdventureWorks--使用数据库goselect count(*) from HumanResources.Employee-- 数据库.架构名.表名go--如何创建架构create schema class11create table class11.Info( int primary identity(,) , nvarchar() not )gocreate schema class12--创建架构的语法gocreate table class12.Info( int not , nvarchar)select * from AdventureWorks.HumanResources.Employeego--查找指定的列select EmployeeID from HumanResources.Employee--列的值升序排列select EmployeeID from HumanResources.Employeeorder by EmployeeID asc--desc --(asc)--查询多少条数据select count(*) as from HumanResources.Employee--创建数据库--创建文件夹create database dataTypeDBon primary( =, =, =, =, =%)log on( =, =, =, =, =)use dataTypeDB--使用数据库create table demoTbl( birthday date not )--drop table demoTbl 删除表--demoTbl 删除表中的数据insert into demoTbl inserted.* values()insert into demoTbl values(--)insert into demoTbl inserted.* values()select * from demoTbldemoTblalter table demoTbl add salary datetime not go--alter 无数据的使用select * from demoTblinsert into demoTbl inserted.* values(,)insert into demoTbl inserted.* values(,)godeclare @datetime@=select @as declare @numeric(,)@=select @as [numeric]--sql 声明变量的时候--declare @变量名 数据类型--设置declare @decimal(,)@=select @as gocreate table test( num decimal(,))--drop table testinsert into test inserted.* values()insert into test inserted.* values()create table testNumeric( num numeric(,))insert into testNumeric inserted.* values()insert into testNumeric inserted.* values()use AdventureWorksgoselect * from HumanResources.Employeeuse dataTypeDBcreate table rongYuTbl( int primary identity(,), char() not )insert into rongYuTbl inserted.* values(),(),()select * from rongYuTblselect distinct() from rongYuTblselect distinct() into newTbl from rongYuTbl--select * from newTbl--drop table newTblnewTbl deleted.* where =select * from newTblselect * from newTbl where =create schema jiagouA--创建架构create schema jiagouBcreate table jiagouA.( int primary identity(,) , nvarchar() not )create table jiagouA.( int primary )create table jiagouB.( int primary identity(,), nvarchar() not )select * from jiagouA.select * from jiagouA.--指定特定列select * from AdventureWorks.HumanResources.Employeeselect .EmployeeID,.Title into Empfrom AdventureWorks.HumanResources.Employee as select * from Empselect EmployeeID as ,as from Empselect =EmployeeID,=from Empselect EmployeeID ,from Empselect EmployeeID , ,,from Empselect EmployeeID , as ,,as from Empselect EmployeeID +++as Infomation from Empselect * from AdventureWorks.HumanResources.Departmentselect .Name ++GroupName+from AdventureWorks.HumanResources.Department as d------------------------------------- select * from AdventureWorks.HumanResources.EmployeeDepartmentHistoryselect S.DepartmentID,S.EmployeeID into Dep from AdventureWorks.HumanResources.EmployeeDepartmentHistory as Sselect * from Depselect DepartmentID+EmployeeID from Depselect * from Depselect * from Dep where DepartmentID>select * from Dep where DepartmentID between and select * from Dep where DepartmentID=or DepartmentID=select * from Dep where DepartmentID=or DepartmentID=select * from Empinsert into Emp output inserted.* values()select * from Emp where Title likeand Title like select * from Emp where Title like or Title like select * from Emp where not Title=- ------------------- select * from AdventureWorks.HumanResources.Employeeselect e.EmployeeID,e.Title,e.LoginID into Emp from AdventureWorks.HumanResources.Employee eselect * from Emp--存储过程--exec sp_rename ,,update Emp set Title =output inserted.* where EID=update Emp set Title =output deleted.* where EID=---------------------------- update Emp set Title =output inserted.* ,, deleted.* where EID=-------- select * from Emp where Title (,,)select * from Emp where Title (,,)select * from Emp where Title not (,,)--------------- select * from AdventureWorks.HumanResources.EmployeeDepartmentHistoryselect E.EmployeeID,E.EndDate,E.StartDate,E.ModifiedDateinto EDH from AdventureWorks.HumanResources.EmployeeDepartmentHistory Eselect * from EDH where EndDate is not select * from EDH where EndDate is select EmployeeID,isnull(EndDate,) as EDate from EDH---------- select EmployeeID,coalesce(EndDate,StartDate,ModifiedDate) as C from EDH--------------------------------- select top * from AdventureWorks.HumanResources.Employeegoselect e.BirthDate,e.HireDate,e.EmployeeID from AdventureWorks.HumanResources.Employee ewhere HireDate>=and HireDate<=order by HireDate desc--ASC-------- select e.EmployeeID,e.NationalIDNumber,e.ContactID,e.HireDateinto EmpNewfrom AdventureWorks .HumanResources.Employee egoselect * from EmpNew order by EmployeeID --descoffset rowsfetch next rows only------ select * from AdventureWorks .HumanResources.Departmentorder by DepartmentID descoffset rowsfetch next rows onlygo---- 五,ASCII表练习使用--row_number()--rank()--dense_rank()--ntile()函数。select =upper()select ascii()select UNICODE()select DIFFERENCE(,)select char()select CHARINDEX(,)select left(,)select RIGHT(,)select len()select lower()select DATEDIFF(year ,convert(datetime,), convert(datetime,))select DATEDIFF(day ,convert(datetime,), convert(datetime,))------------------- --row_number()--rank()--dense_rank()--ntile()select .EmployeeID,.Rate,rank() over (order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory select .EmployeeID,.Rate,row_number() over (order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory select .EmployeeID,.Rate,dense_rank() over (order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory goselect .EmployeeID,.Rate,dense_rank() over (partition by EmployeeID order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory order by EmployeeID ascoffset rowsfetch next rows only-------------------------select =upper()select ascii()select UNICODE()select DIFFERENCE(,)select char()select CHARINDEX(,)select left(,)select RIGHT(,)select len()select lower()select DATEDIFF(year ,convert(datetime,), convert(datetime,))select DATEDIFF(day ,convert(datetime,), convert(datetime,))---------------------row_number()--rank()--dense_rank()--ntile()select .EmployeeID,.Rate,rank() over (order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory select .EmployeeID,.Rate,row_number() over (order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory select .EmployeeID,.Rate,dense_rank() over (order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory goselect .EmployeeID,.Rate,dense_rank() over (partition by EmployeeID order by rate desc) as RANKDemofrom AdventureWorks.HumanResources.EmployeePayHistory order by EmployeeID asc
SQL Server基础SQL脚本之创建架构、排序
来源:本站原创 浏览:94次 时间:2022-11-09
推荐资讯
- PHP - Manual: event_base_new
- PHP - Manual: magic_quotes_runtime
- PHP - Manual: HaruPage::setLineCap
- PHP - Manual: MysqlndUhConnection::killConnection
- PHP - Manual: dbplus_xunlockrel
- PHP - Manual: gupnp_root_device_start
- PHP - Manual: MysqlndUhConnection::getStatistics
- PHP - Manual: MysqlndUhConnection::connect
- PHP - Manual: Beyond TTL: user-defined storage
- PHP - Manual: CairoStatus
推荐站点
- At-lib分类目录
At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家
www.at-lib.cn - 中国链接目录
中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。
www.cnlink.org - 35目录网
35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。
www.35mulu.com - 就要爱网站目录
就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。
www.912219.com - 伍佰目录
伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。
www.wbwb.net