SQL Server basic syntax example (1)

Source: Internet
Author: User
I. Basics 1. Description: Create a database? 1CREATEDATABASEdatabase-name2. Description: delete a database? 1DROPDATABASEdatabase-name3. Description: backup database? 12345USEmaster -- create deviceEXECsp_addumpdevicedisk, cc_jz, d: cc_jz.dat --

I. Basics 1. Description: Create a database? 1 create database database-name 2. Note: delete a DATABASE? 1 drop database database-name 3. Description: backup DATABASE? 1 2 3 4 5 USE master -- create device EXEC sp_addumpdevice 'disk', 'CC _ jz', 'd: cc_jz.dat '--

I. Basics 1. Description: Create a database

?

1

Create database database-name

2. Description: delete a database.

?

1

Drop database database-name

3. Description: Back up the database

?

1

2

3

4

5

USE master

-- Create a device for the backup data

EXEC sp_addumpdevice 'disk', 'CC _ jz', 'd: cc_jz.dat'

-- Start backup

Backup database cc_jz TO cc_jz

4. Description: Create a new table.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Create table tabname (col1 type1 [not null] [primary key], col2 type2 [not null],...)

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int, [product name] varchar (6), [number of incoming records] int, [date of receipt] datetime)

Insert [a]

Select 1, 'Mineral Water', 100, '2017-01-02 'union all

Select 2, 'Instant noodle ', 60, '2017-01-03' union all

Select 3, 'Instant noodle ', 50, '2017-01-03' union all

Select 4, 'Mineral Water', 80, '2017-01-04 'union all

Select 5, 'Instant noodle ', 50, '2017-01-05'

Select * from

/*

ID product name warehouse receiving time

---------------------------------------------------

1 mineral water 100 00:00:00. 000

2 instant noodles 60 00:00:00. 000

3 instant noodles 50 00:00:00. 000

4 mineral water 80 00:00:00. 000

5 instant noodles 50 00:00:00. 000

(5 rows affected)

*/

5. Description: delete a new table.

?

1

Drop table tabname

6. Description: Add a column.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Alter table tabname add column col type

Alter table a add col int

Select * from

/*

ID product name warehouse receiving quantity warehouse receiving time col

--------------------------------------------------------------

1 mineral water 100 00:00:00. 000 NULL

2 instant noodles 60 00:00:00. 000 NULL

3 instant noodles 50 2013-01-03 00:00:00. 000 NULL

4 mineral water 80 2013-01-04 00:00:00. 000 NULL

5 instant noodles 50 2013-01-05 00:00:00. 000 NULL

(5 rows affected)

*/

7. Description: Add a primary key:

?

1

Alter table tabname add primary key (col)

Note: To delete a primary key:

?

1

Alter table tabname drop primary key (col)

8. Description: Create an index:

?

1

Create [unique] index idxname on tabname (col ....)

Delete An index:

?

1

Drop index idxname

Note: The index cannot be changed. To change the index, you must delete it and recreate it.

9. Description: Create a view:

?

1

Create view viewname as select statement

Delete View:

?

1

Drop view viewname

10. Description: several simple basic SQL statements

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

-- Select:

Select * from table1

-- Insert:

Insert into table1 (field1, field2) values (value1, value2)

-- Delete:

Delete from table1 -- where range

-- Update:

Update table1 set field1 = value1 -- where range

-- Search:

Select * from table1 where field1 like '% value1 %'

-- Sort:

Select * from table1 order by field1, field2 [desc]

-- Total:

Select count as totalcount from table1

-- Sum:

Select sum (field1) as sumvalue from table1

-- Average:

Select avg (field1) as avgvalue from table1

-- Maximum:

Select max (field1) as maxvalue from table1

-- Minimum:

Select min (field1) as minvalue from table1

11. Description: several advanced query Operators

A: UNION/union all operator

The UNION operator combines two other result tables (such as TABLE1 and TABLE2) and removes any duplicate rows from the table to generate a result table.

When ALL is used together with UNION (that is, union all), duplicate rows are not eliminated. In either case, each row of the derived table is from either TABLE1 or table2.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int)

Insert [a]

Select 1 union all

Select 1 union all

Select 2 union all

Select 3 union all

Select null

Select * from

/*

(5 rows affected)

ID

-----------

1

1

2

3

NULL

(5 rows affected)

*/

--> Test data: [B]

If object_id ('[B]') is not null drop table [B]

Go

Create table [B] ([ID] int)

Insert [B]

Select 1 union all

Select 2 union all

Select 2 union all

Select 4 union all

Select null

Select * from B

/*

(5 rows affected)

ID

-----------

1

2

2

4

NULL

(5 rows affected)

*/

-- Merge and remove duplicates

Select * from

Union

Select * from B

/*

ID

-----------

NULL

1

2

3

4

(5 rows affected)

*/

-- Merge without duplicates

Select * from

Union all

Select * from B

/*

ID

-----------

1

1

2

3

NULL

1

2

2

4

NULL

(10 rows affected)

*/

B: Random t operator

The distinct t operator derives a result table by including all rows in Table 1 but not in table 2 and eliminating all repeated rows.

Note: There is no limit t ALL usage at ALL. Many articles on the Internet contain limit t all, which is actually incorrect. (Test SQL Server 2000 2005 2008R2 2012 is not applicable)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int)

Insert [a]

Select 1 union all

Select 1 union all

Select 2 union all

Select 3 union all

Select null

Select * from

/*

(5 rows affected)

ID

-----------

1

1

2

3

NULL

(5 rows affected)

*/

--> Test data: [B]

If object_id ('[B]') is not null drop table [B]

Go

Create table [B] ([ID] int)

Insert [B]

Select 1 union all

Select 2 union all

Select 2 union all

Select 4 union all

Select null

Select * from B

/*

(5 rows affected)

ID

-----------

1

2

2

4

NULL

(5 rows affected)

*/

-- Take two different tables and remove duplicates

Select * from

EXCEPT

Select * from B

/*

ID

-----------

3

(One row is affected)

*/

C: INTERSECT Operator

The INTERSECT operator derives a result table by only including the rows in TABLE1 and TABLE2 and eliminating all repeated rows.

Note: There is no intersect all usage at ALL. Many articles on the Internet contain intersect all, which is actually incorrect. (Test SQL Server 2000 2005 2008R2 2012 is not applicable)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int)

Insert [a]

Select 1 union all

Select 1 union all

Select 2 union all

Select 3 union all

Select null

Select * from

/*

(5 rows affected)

ID

-----------

1

1

2

3

NULL

(5 rows affected)

*/

--> Test data: [B]

If object_id ('[B]') is not null drop table [B]

Go

Create table [B] ([ID] int)

Insert [B]

Select 1 union all

Select 2 union all

Select 2 union all

Select 4 union all

Select null

Select * from B

/*

(5 rows affected)

ID

-----------

1

2

2

4

NULL

(5 rows affected)

*/

-- Two tables with the same data and deduplication

Select * from

INTERSECT

Select * from B

/*

ID

-----------

NULL

1

2

(3 rows affected)

*/

12. Note: use external connections
A. left (outer) join:
Left Outer Join (left join): the result set contains the matched rows in the connected table, and all rows in the left connected table.

SQL: select a. a, a. B, a. c, B. c, B. d, B. f from a left out join B ON a. a = B. c

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int)

Insert [a]

Select 1 union all

Select 1 union all

Select 2 union all

Select 3 union all

Select null

Select * from

/*

(5 rows affected)

ID

-----------

1

1

2

3

NULL

(5 rows affected)

*/

--> Test data: [B]

If object_id ('[B]') is not null drop table [B]

Go

Create table [B] ([ID] int)

Insert [B]

Select 1 union all

Select 2 union all

Select 2 union all

Select 4 union all

Select null

Select * from B

/*

(5 rows affected)

ID

-----------

1

2

2

4

NULL

(5 rows affected)

*/

Select a. *, B. * from a left join B ON a. id = B. id

/*

ID

----------------------

1 1

1 1

2 2

2 2

3 NULL

NULL

(6 rows affected)

*/

B: right (outer) join:

Right Outer Join (right join): the result set includes both matched join rows in the connection table and all rows in the right join table.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int)

Insert [a]

Select 1 union all

Select 1 union all

Select 2 union all

Select 3 union all

Select null

Select * from

/*

(5 rows affected)

ID

-----------

1

1

2

3

NULL

(5 rows affected)

*/

--> Test data: [B]

If object_id ('[B]') is not null drop table [B]

Go

Create table [B] ([ID] int)

Insert [B]

Select 1 union all

Select 2 union all

Select 2 union all

Select 4 union all

Select null

Select * from B

/*

(5 rows affected)

ID

-----------

1

2

2

4

NULL

(5 rows affected)

*/

Select a. *, B. * from a right join B ON a. id = B. id

/*

ID

----------------------

1 1

1 1

2 2

2 2

NULL 4

NULL

(6 rows affected)

*/

C: full/cross (outer) join:

Full outer join: includes not only matching rows in the symbolic join table, but also all records in the two join tables.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

--> Test data: [a]

If object_id ('[a]') is not null drop table [a]

Go

Create table [a] ([ID] int)

Insert [a]

Select 1 union all

Select 1 union all

Select 2 union all

Select 3 union all

Select null

Select * from

/*

(5 rows affected)

ID

-----------

1

1

2

3

NULL

(5 rows affected)

*/

--> Test data: [B]

If object_id ('[B]') is not null drop table [B]

Go

Create table [B] ([ID] int)

Insert [B]

Select 1 union all

Select 2 union all

Select 2 union all

Select 4 union all

Select null

Select * from B

/*

(5 rows affected)

ID

-----------

1

2

2

4

NULL

(5 rows affected)

*/

Select a. *, B. * from a full join B ON a. id = B. id

/*

ID

----------------------

1 1

1 1

2 2

2 2

3 NULL

NULL

NULL 4

NULL

(Eight rows are affected)

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.