常用基本SQL语句
? --常用Sql
CREATE DATABASE database-name;
DROP DATABASE database-name;
create table?depart?(dept_id?int(11) NOT NULL AUTO_INCREMENT,?
dept_name?varchar(255) DEFAULT NULL, PRIMARY KEY (dept_id));?
根據已有的表創建新表:?
create table tab_new like tab_old (使用舊表B創建新表A)?
備注:此種方式在將表B復制到A時候會將表B完整的字段結構和索引復制到表A中來?
create table tab_new as select col1,col2… from tab_old definition only?
備注:此種方式只會將表B的字段結構復制到表A中來,但不會復制表B中的索引到表A中來。這種方式比較靈活可以在復制原表表結構的同時指定要復制哪些字段,并且自身復制表也可以根據需要增加字段結構。?
create table as select 會將原表中的數據完整復制一份,但表結構中的索引會丟失。?
create table like 只會完整復制原表的建表語句,但不會復制數據。
drop table tabname;
alter table tabname add column column_name type
說明:刪除主鍵:?Alter table tabname drop primary key?
一個數據表只可以有一個主鍵,所以不存在刪除某一列的主鍵.
刪除索引:drop index idxname?
注:索引是不可更改的,想更改必須刪除重新建。
刪除視圖:drop view viewname
選擇:select * from table1 where 范圍?
插入:insert into table1(field1,field2) values(value1,value2)?
刪除:delete from table1 where 范圍?
更新:update table1 set field1=value1 where 范圍?
查找:select * from table1 where field1 like ’%value1%’ —like的語法很精妙,查資料!?
排序:select * from table1 order by field1,field2 [desc]?
desc:降序,asc:升序?
總數:select count as totalcount from table1?
求和:select sum(field1) as sumvalue from table1?
平均:select avg(field1) as avgvalue from table1?
最大:select max(field1) as maxvalue from table1?
最小:select min(field1) as minvalue from table1
一張表,一旦分組完成后,查詢后只能得到組相關的信息。?
組相關的信息:(統計信息)?count,sum,max,min,avg 分組的標準)
select * from table1 where time between time1 and time2?
select a,b,c, from table1 where a not between 數值1 and 數值2
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
?
轉載于:https://www.cnblogs.com/Auraro/p/7217928.html
總結
- 上一篇: 正则表达式里转义字符_五分钟搞定正则表达
- 下一篇: 最好用的Redis客户端