当前位置:网站首页>Common SQL statements

Common SQL statements

2022-06-09 22:37:00 Versatile senior

Commonly used SQL sentence :

1、 Create database

CREATE DATABASE database-name;

3、 Delete database

DROP DATABASE database-name;

5、 Create new table

create table depart (dept_id int(11) NOT NULL AUTO_INCREMENT, 
dept_name varchar(255) DEFAULT NULL, PRIMARY KEY (dept_id)); 

8、 Create a new table from an existing table :

create table tab_new like tab_old ( Use old table B Create new table A) 

10、 remarks : In this way, the table B Copied to the A When will the watch B Copy the complete field structure and index to the table A In the to

create table tab_new as select col1,col2… from tab_old definition only 

12、 remarks : In this way, only the table B The field structure of is copied to the table A In the to , But the table will not be copied B Index to table in A In the to . This method is flexible. You can specify which fields to copy while copying the original table structure , You can also add field structure to the self copying table as needed .
create table as select A complete copy of the data in the original table , But the indexes in the table structure will be lost .
create table like Only the table creation statement of the original table will be copied completely , But the data will not be copied .
15、 Delete new table

drop table tabname;

17、 Add a column

alter table tabname add column column_name type
19、 Add primary key : Alter table tabname add primary key(col) 
20、 Delete primary key : Alter table tabname drop primary key 
21、 A data table can only have one primary key , Therefore, there is no primary key to delete a column .
22、 Create index :create [unique] index idxname on tabname(col….) 
23、 Delete index :drop index idxname 
24、 notes : The index is immutable , If you want to change it, you must delete and rebuild .
25、 Create view :create view viewname as select statement 
26、 Delete view :drop view viewname
27、 A few simple basic sql sentence  
28、 choice :select * from table1 where  Range  
29、 Insert :insert into table1(field1,field2) values(value1,value2) 
30、 Delete :delete from table1 where  Range  
31、 to update :update table1 set field1=value1 where  Range  
32、 lookup :select * from table1 where field1 like ’%value1%33、 Sort :select * from table1 order by field1,field2 [desc] 
34、desc: Descending ,asc: Ascending  
35、 total :select count as totalcount from table1 
36、 Sum up :select sum(field1) as sumvalue from table1 
37、 Average :select avg(field1) as avgvalue from table1 
38、 Maximum :select max(field1) as maxvalue from table1 
39、 Minimum :select min(field1) as minvalue from table1
40、 grouping :Group by: 
41、 A watch , Once the grouping is complete , Only group related information can be obtained after query . 
42、 Group related information :( Statistics ) count,sum,max,min,avg 
43、 Copy table select * into b from a where 1<>1
44、 Subquery ( Table name 1:a  Table name 2:b) select a,b,c from a where a IN (select d from b ) 
45、 Show article 、 The author and the final response time  
46、select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
47、 View query ( Table name 1:a ) 
48、select * from (SELECT a,b,c FROM a) T where t.a > 1;
49、between Usage of ,between The boundary value is included when the query data range is limited ,not between barring  
50、select * from table1 where time between time1 and time2 
51、select a,b,c, from table1 where a not between  The number 1 and  The number 2
52、in  How to use  
53、select * from table1 where a [not] in (‘ value 1,’ value 2,’ value 4,’ value 6)
54、 Two related tables , Delete the information in the primary table that is not in the secondary table  
55、delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
56、 Schedule five minutes in advance to remind  
57、SQL: select * from  Schedule  where datediff(‘minute’,f Starting time ,getdate())>5
58、 front 10 Bar record  
59、select top 10 * form table1 where  Range 
60、 Choose in each group b The data with the same value corresponds to a The biggest record of all information ( Similar usage can be used for forum monthly leaderboards , Monthly hot product analysis , Rank by subject score , wait .) 
61、select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
62、 Including all in  TableA  But not in  TableB and TableC  The result table is derived by eliminating all duplicate rows  
63(select a from tableA ) except (select a from tableB) except (select a from tableC)
64、 Random take out 10 Data   select top 10 * from tablename order by newid()
原网站

版权声明
本文为[Versatile senior]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092157112334.html