The basic statement sql server

Sql is a structured query language;
Sql is a programming language and database query for accessing data and query, update and manage 'relational database' system;
Sql case insensitive;
Sql not a database, is the industry standard structured query language



Int
Char (30) - the field can only accept 30 or 15 Chinese letters
Varchar (30)

. 1) with frequently changing fields VARCHAR;
2) with known fixed length char;
. 3) as possible with VARCHAR;
. 4) than use only 255 byte VARCHAR;

 

Value: int (integer type);
all strings must be enclosed in single quotes;


Sql statement:
1, Create Database database_name; - creating a database
2, use database_name; - references database
3, create table table_name (Column name Data type 1 ,,
  Column name Data type 2, ... ..
  Column Name Data Type n) ; - create table
4, select * from table name; - information for all columns in the table view
  select 1 column names, column names 2 from table name;
  select statement is used to select data from the table.
5, the constraint: Not null
  of Unique unique constraints - no redundant data
  Primary key - must contain unique values, the primary key column can not contain a null value 
6, drop table table_name; - delete Table
7, insert into table values (values 1 , the value 2, ...); - the column value must correspond
  insert into table (column 1, row 2 ...) values (value 1, value 2, ...);
. 8, the aS - alias for the table name and column names
  Select new column as the column name from the name table; - column aliases
. 9, where - the conditional statement
  select column names from table where column (operator) = 'value';
10, single line comment -
  multi-line comment / * .... * /
11, modify the data structure
  1) alter table table alter column Column Name Data Type; - modify the data structure (column attribute modification)
  Example: alter table Country table name VARCHAR alter column (250);
  ALTER Table table alter column country varchar (250) null ;
  2) ALTER table table add constraint constraint name unique (column constraints);
  Alter table table drop constraint constraint name;
  . 3) ALTER table table add column column name data type;
  . 4) ALTER table table drop column column name ;
12, modify the data, updating
  update table set column name = new value where the column name = a certain value;
13 is,. 1) BETWEEN ... and ...
  the Select column names from table where column names BETWEEN value 1 and value 2;
  the Select columns name from table where column names between 'string 1' and 'string 2';
  2) and Not BETWEEN ... ...
14,. 1) like - fuzzy search
  only, and where with the use of wildcard "%" and the like are connected only use
  Select column names from table where column name like 'n-%' / '% n-%' / 'n-%'
  2) Not like
15, and or 
  . 1) and for the operator and based on one or more filtering conditions recorded;
  2) and combines and or may be two or more conditions in the where the statement;
  3) if two conditions are true, then a record display and operator;
  4) If there is a long two conditions is satisfied, a recording or operator display;
  . 5) and the position and or in no particular order sql statement, query results are consistent ;
  6) execution rules: first calculating and computing operator or operator;
16, in

  1) In one set inside, with () comprising, in brackets query columns, separated by commas
  2) string type single quotation marks; - allowing a predetermined plurality of values in the where clause
  3) does not match with in conjunction operator;
  the Select from table where the column names in the column name (value 1, value 2, ....);
. 17, DISTINCT - only supports single
  Select distinct (column names) from table;
18 is, by Order - sorting when a condition satisfies the first condition of the first column
  1) sort the result set for the specified column
  2) to sort the records in ascending default-ASC
  . 3) -desc descending
  elevator ordering 4) letters, numbers can be freely
  5) allows sorting a plurality of columns, but from left to right sort priority
19, top - a predetermined number of records to be returned to
  1) top 6 - before. 6
  2) Top. 6 desc - reciprocal 6
  3) top 50 percent - the first part
  4) top50 percent desc- latter half
20, delete - delete data (a table will be deleted, or all rows)
  - can only delete entire row of data, not delete the specified cell data.
  Delete from table where column name = value;
21 is, Identity - Growth from
  Example: Identity ID int
  Id int Identity (1000,1)
22, check constraints
  - means for limiting the range of values in a column
  Create table table name (name VARCHAR (100),
  Age INT CHECK (aged> = 18 AND Age <= 30), - OR can not be used where
  gender CHAR ( 4) CHECK (sex = 'M' OR gender = 'F') - can not be used where the AND
  );
23 is, default constraint - for inserting a default value to the column
  if the column is no longer constrained additionally have default values input, then the default value is automatically filled in; the other hand, the input value is filled;
  Example: the CREATE TABLE INSERT_DEFAULT_EXAM_DATE
  (
  Id the INT the IDENTITY a PRIMARY KEY,
  name VARCHAR (100) NOT NULL,
  the examination time DATE dEFAULT '2018-05-20 '
  );
24 except default and check constraints
  . 1) can only enter the mandatory check constraint of the column containing the value of the constraint constraint;
  2) relatively free default constraints, if the input value is to take effect; if (allowed) do not enter value, the default value is set and inserted in the table columns.
25, null - must be used is null and not null IS
  1) the SELECT * from table where column name is null; (column called space-time, view information)
  2) select * from table where column name is not null; time (column name is not empty, to view the information)
26, built-in functions for calculating and computing
  . 1) SUM
  2) Avg. - returns the average value of the column / null value is not included in the calculation
  Select avg (column name) from name;
  Example: SELECT name fROM software testing exam score table _ heavy plate
  WHERE QTP score> (SELECT AVG (QTP score) fROM software testing exam score table _ weight plate);

27, isnull - NULL value for specifying how to handle
  Example: select name, (theoretical test scores + isnull (sql score, 0) + QTP Score Score + Selenium) as test subjects score from table column name = value where ;
28,1) max () - returns the largest value in a column
  Select max (column name) from name;
  Example: select max (QTP score) as QTP highest score from table where class = 'class';
  2) min () - returns the minimum value of a
  Select min (column name) from name;
  Example: select name from the table where SQL score = (select min (SQL score) from the table);
  . 3) the Sum () - returns Total numerical sequence summing only for digital type
  Select sum (column names) from table;
  . 4) Avg. ()
  Note: max and min text columns may also be used to obtain the highest or lowest value in alphabetical order.
29, count - count of the number of rows returned matches the specified criteria
  1) Select count (*) from table; - Returns the number of records in the table
  1 case: select count (*) from the table; - all records lookup table
  2) Select count (column names) from table; - returns the number of the specified column of
  Example 2: select count (name) as the number of students from class = class table where 'class';
  - specified column to count
  3) select count (distinct column name) from name; - Returns the number of different values of the specified column
  three cases: select count (distinct class) from the table; - show unique recording 
  a unique key -distinct
30, len - length - returns the value of the field length of the text length
  the Select len (column name) from name;
  Example 1: select len (test sites) as the length of the test sites from table where name = 'John Doe';
31, no association polyepitopic query
  1) select * from (SELECT name, QTP score fROM software testing exam scores table _ remake 
  WHERE QTP score> (SELECT AVG (QTP score) fROM software testing exam scores table _ remake)) AS a temporary table 1,
  (the SELECT AVG (QTP score) AS average FROM test the QTP testing software test score table _ heavy plate) AS temporary table 2;
  2) the SELECT temporary table 1. name, temporary table 1.QTP score, average temporary table 2.QTP examination FROM 
  (SELECT name, QTP score FROM software testing exam scores table _ remake 
  WHERE QTP score> (SELECT AVG (QTP score) FROM software testing exam scores table _ remake)) AS a temporary table 1,
  (SELECT AVG (QTP score) AS QTP test measured average FROM software Test test scores table _ heavy plate) AS a temporary table 2;
  Example 1: - Query student's name and qtp score and the average score is displayed qtp
  Select * from (select name, QTP score from table where QTP score> (select avg (QTP score) as average from table)) as table1, (select avg (QTP score) as average from table) as Table2;
32 , 1) foreign key (constraint) References - adding multiple foreign key constraint
  - a foreign key in the table points to a primary key in another table.
  - foreign key can only be connected to the primary key of that table you want to connect (normally primary key id)
  Example: CREATE TABLE table class
  (Class_Id INT PRIMARY KEY,
  class VARCHAR (10) the NOT NULL
  );
  the CREATE TABLE Student table
  (Student_Id INT PRIMARY kEY,
  name VARCHAR (10),
  student_Class_Id the INT a fOREIGN kEY the REFERENCES class table (class_id)
  );
  2) increased individual foreign key constraint
  Example 1: alter table table students add foreign key (student_class_id) references the class table (class_id);
  . 3) increase the number of constraints
  Example 2: alter table Student table add constraint constraint name foreign key (student-class_id) references the class table (class_id);
  . 4) revoke constraint
  Example 3: alter table Student table drop constraint constraint name;
  Example 4: select * from the class table, student class table where the table .class_id = table students .student_class_id;
33 is, group by
  - aggregate function (Example sum, max, min, len) often need to add a group by statement
  1) are grouped in a single column
  Select column name, aggregate functions ( column name) from table where ... group by column names;
  Example: select the store counter, sum (sale price) as Valentine's day perfume total sales for the day
  from table where sales time = '2018-02-14'group by mall Valentine perfume counter order by total sales desc day;
  2) grouping a plurality of columns
  Example: select the sales period, the store counter, sum (sales price) as the total sales for the day counter perfume from table 
  Group by selling time, shopping malls counters
  Order by sale time desc, shopping malls counters asc;

34,1) group by ... .having ...
  Example 1: select the store counter, sum (sales price) as total sales from table 
  Group by the store counter having sum (sales price)> 600 order by total sales desc;
  Example 2: select the store counter, sales period, sum (sales price) as total sales from table
  Group by the store counter, sales period having sum (sales price)> 500 order total sales by desc;
  2) WHERE may also be used in the group by
  Example 1 : Select store counter, sum (sales price) from table where the sales period = '2018-02-14' group by having the store counter sales price> 500
  the Order by SUM (sales price) desc;
  Example 2: select top 1 perfume No. _ foreign key, count (number _ perfume foreign key) from the sales table where time = '2018-02-14' group by number _ perfume foreign key order by count (number _ perfume foreign key) desc;
  Example 3: select the store counter, selling time, count (number _ perfume foreign key) as perfume sales where the sales period in ( '2018-02-14', '2018-03-14 ') and len ( No. _ perfume foreign key) <= 4 from table Group sales by time order by coun T (No Perfume _ foreign key) desc;
35, connector
  1) is connected - (inner) join - is connected through the primary and foreign keys
  - there is matching data (matching the primary key and the foreign key) in between the tables to the query, the query does not match the data is less than, equal to find two tables associated row.
  select * from Table 1 inner join Table 2 on Table 1.A = Table 2.A;
  Example 1: select the store counter, selling time, owned the brand, the name perfume, perfume sales price information from table inner join table on the store counter sales .. No perfume fragrance information table _ = foreign key table perfume store counter sales order by number _ foreign key store counter asc, sales time desc;
  2) left join the Join --left
  - left the table where all rows are returned, even if there is no match in the right table rows
  Select from table column names. 1 left join on table 2 table 1.A = table 2.A and other conditions;
  Example: select * from perfume information table left join on the store counter sales table perfume perfume information table No. _ = foreign key store counter sales Fact sheet No. _ perfume foreign key (perfume information table all records).
  3) the Join right connection --right 
  - right table return all rows, not even in the left table matching row
  Select from table column names. 1 right join on table 2 table 1.A table 2.A. =
  Example: select * from perfume information table right join table on the store counter sales information table perfume perfume _ ID = foreign key. store counter sales table. No perfume _ foreign key ( Store counter sales display table all records) se

Guess you like

Origin www.cnblogs.com/dongweichang/p/11284741.html