SQL basic syntax and writing format

Insert
insert [into] table [(column name list)] values (value list)

insert into the new table name (column name list) select list of column names from the table name

select the table name. table column names into a new name from the table name

insert table (column name list) select a list of values ​​1 union select values ​​from the list


Modify
update table column names set update value = [where <update condition>]

update table column names set update value = 1, n = updated value of the column name [where <update condition>]


Delete
delete [from] table [where <Delete condition>]

truncate table 表名


Query
select a column name from table where <query expression>
the Order by <sort column name> asc or desc

select * from table name // query all data

select list of column names from the query // table where the query part of a row or column

select the column names as column aliases from table where the query // use a column alias

select * from table where the column name is null null // query

select a column alias = column names, column names as default information from table // use constants

Rows column name from table where the query select top display // limit the number of rows displayed

select top digital percent column names from the query // table where the percentage extraction

Fuzzy query
select * from table where column names like 'query% value'

select * from table where column names between the initial value and final value

select * from table where column names in (value 1, value 2)


Grouped query
select a column name from table group by column name


Connecting the
SELECT table 1. Column names, table names, column names FROM table 2. 1, table 2
the WHERE column name table 1. table 2. Column name =

1. SELECT table column names, table names, column names FROM table 2. 1
the INNER Name the JOIN Table 2 ON (= table column name table 1. 2. Column name)

Left outer
SELECT table 1. Column names, table names, column names FROM table 2. 1
the LEFT OUTER Name the JOIN Table 2 ON = column name table 1. table 2. Listed

Right outer join
SELECT table 1. Column names, table names, column names FROM table 2. 1
RIGHT 2 ON OUTER the JOIN table column name table 1. table 2. Column name =

 

Wildcard
-: a character

%: arbitrary length string

[]: a character in the range specified in brackets

[^]: not in parenthesis in the range of one arbitrary character designation


Aggregate functions
the SUM () the sum of
the AVG () average
maximum value MAX ()
MIN () minimum value
COUNT () Count
String Functions

charindex search for the specified string select charindex ( 'a', ' name')
of another home position return string 2

string length len select len ​​( 'name') returns 4

upper turn uppercase select upper ( 'name') returns NAME

ltrim cleared the left-character space select upper ( 'name')
returns "name"

rtrim remove the right character spaces select upper ( 'name')
returns "name"

Returns the right SELECT right specified number of characters from the right string ( 'name', 2) to return me

Alternatively replace a character string select replace ( 'name', ' a', 'b')
returns nbme

stuff in a string, select stuff ( 'name', 2,3, 'bsp')
to delete the specified character length, returns nbsp
and insert a new character at the position


Date Functions

getdate get the current system message select getdate () returns the current date

dateadd the specified value to the specified select dateadd (mm, 4, '01 / 01/2009 ')
the date after the date 05/01/2009 return portion

SELECT datediff datediff interval between two dates
(mm, '01 / 01/2009 ',' 05/01/2009 ')
returns 4

datename date specified date select datename portion (dw, '01 / 01/2000 ')
returned as a string or Saturday, Saturday

integer portion of the date specified datepart date select datepart (day, '01 / 15 /2009 ')
returns 15

Some parameters and date abbreviations

year yy, yyyy year
quarter qq, q season
month mm, m month
dayofyear dy, y day of the year,
day dd, d days
week wk, ww first weeks
weekday dw, w of week
hour hh, h hour
minute mi, n sub
second ss, s seconds
minllisecond ms ms

 

Mathematical Functions

Returns from the random value rand float between 0-1 SELECT rand () 0.79854628433

abs takes the absolute value of the numerical expression select abs (-43) returns 43

ceiling rounding up, take greater than or equal select ceiling (43.5) 44 returns the
specified value, the smallest integer expression

floor rounding down, taking less than or equal to select floor (43.5) 43 Returns
the specified maximum integer expression

exponential value select power (5,2) power takes a value expression returns 25

The numeric expression round rounded to the specified precision select round (43.54,1) returns 43.50

positive sign and +1, -1, 0, negative returns 0 select round (-43) -1

taking the square root of a floating-point expression sqrt select sqrt (9) returns 3


System functions

convert the data type conversion select convert (varchar (5), 12345)
returns 12345

current_user returns the current name of the user select current_user ()
Returns the user name of your landing

datalenght Returns the number of bytes specified expression select datalenght ( 'name') returns 4

host_name Returns the computer name of the current user logged on select host_name ()

system_user return the user name currently logged select system_user ()

user_name Returns the user name select user_name (1) from the given user ID
Return "dbo" from any database

data type
binary non-fixed-length binary character and text
varbinary variable length
image Image

Character letters, symbols, numbers, fixed-length non-Unicode character char data
varchar variable-length non-Unicode character data
nchar fixed-length Unicode character data
nvarchar Unicode character data variable-length
text length text information
ntext variable length of the text information

Date and time of the date, time datetime 1753-1-1 to 9999-12-31, accuracy thirty-one seconds per 3.33 milliseconds or

digital positive, negative, fractional integer int
smallint The
tinyint
BIGINT
a float floating-point
real

Decimal currency value currency, fine money
accurate to 4 decimal

bit indicates Yes / No bit Boolean data type

Guess you like

Origin www.cnblogs.com/bugo/p/10994407.html