SQL Server Practice

Source: Internet
Author: User
Tags bulk insert getdate rand

1. Basic Table Exercises:

Create TableTest (Namevarchar(4), ageint, Sexvarchar(2))Alter TableTestAddIdChar( -)Alter TableTestAlter columnIdvarchar( -)Alter TableTestDrop columnIDDrop TableTestSelect *  fromTest

2. Record Operation DML:

Create TableTest (Namevarchar(4), ageint, idChar( -))Alter TableTestAlter columnNamevarchar(6) Insert  intoTestValues('brother Choi .', the,'311409060209');--Note single quotation marksDelete  fromTestwhereName='brother Choi .'UpdateTestSetAge= - whereName='brother Choi .'Bulk InsertTest from 'C:\Users\Administrator\Desktop\test.txt' with(FieldTerminator=' ', Rowterminator='\ n')SelectName fromTestwhere LEN(ID)=7 Order  by Age--desc ASCSelect *  fromTest

3.function usage:

--declare @a int, @b int--Print rand () *26+65--Select @a=cast (' 123 ' as int), @b=convert (int, ' 456 ');--Print @b--print ASCII (' A ')--Print char (rand () *26+65)--Print substring (' John Doe ',;--SELECT * from Test--where Left (name,1) = ' Cui ' or right (name,1) = ' three ' or substring (name,2,1) = ' Four 'Print getdate()Print DateAdd( Day,2,getdate())Print DateAdd(Minute,2,getdate())Print DateAdd(Second,2,getdate())Print DateAdd(Hour,2,getdate())Print DateDiff(Second,getdate(),DateAdd(Hour,2,getdate()))Print DatePart(Week,getdate())-DatePart(Week,DateAdd(Month,-1,getdate()))Print Str(1.25,3,1)+'ASD'--Print str (parameter 1, parameter 2, parameter 3)--The parameter 2 represents the total number of digits of the converted data, including the decimal point, the sign--Parameter 3 indicates the number of decimal digits of the converted data

4.bulk Insert Usage:

Bulk Insert  from'C:\Users\Administrator\Desktop\test.txt'with(    FieldTerminator = ' ', rowterminator='\ n')

5.cursor usage:

--DECLARE mycursor scroll cursor for SELECT * FROM TestOpenMyCursorDeclare @name varchar(6),@age int,@id Char( -)FetchFirst fromMyCursor into @name,@age,@idPrint @namePrint @agePrint @id while @ @FETCH_STATUS=0begin    Fetch Next  fromMyCursor into @name,@age,@id    --Absolute Ten    Print @name    Print @age    Print @idEndCloseMyCursordeallocateMyCursor

6.primary key,unique,not null, check usage:

Alter TableTestDrop columnnameSelect *  fromTestDelete  fromTestSelect *  fromTestAlter TableTestAddNamevarchar(6)Primary KeyAlter TableTestAlter columnIdvarchar( -) not NULLAlter TableTestDrop column AgeAlter TableTestAddAgeint Check(age>=0  andAge<= -)

7.view and Index:

Create ViewMyView as Select *  fromTestSelect *  fromMyViewSelect *  fromMyViewwhereAge= -Create Clustered IndexMyindex onTest (age)Alter IndexMyindex onTest RebuildDrop IndexTest.myindex

8.trigger usage:

SQL Server Practice

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.