常用基本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));?
根據(jù)已有的表創(chuàng)建新表:?
create table tab_new like tab_old (使用舊表B創(chuàng)建新表A)?
備注:此種方式在將表B復(fù)制到A時候會將表B完整的字段結(jié)構(gòu)和索引復(fù)制到表A中來?
create table tab_new as select col1,col2… from tab_old definition only?
備注:此種方式只會將表B的字段結(jié)構(gòu)復(fù)制到表A中來,但不會復(fù)制表B中的索引到表A中來。這種方式比較靈活可以在復(fù)制原表表結(jié)構(gòu)的同時指定要復(fù)制哪些字段,并且自身復(fù)制表也可以根據(jù)需要增加字段結(jié)構(gòu)。?
create table as select 會將原表中的數(shù)據(jù)完整復(fù)制一份,但表結(jié)構(gòu)中的索引會丟失。?
create table like 只會完整復(fù)制原表的建表語句,但不會復(fù)制數(shù)據(jù)。
drop table tabname;
alter table tabname add column column_name type
說明:刪除主鍵:?Alter table tabname drop primary key?
一個數(shù)據(jù)表只可以有一個主鍵,所以不存在刪除某一列的主鍵.
刪除索引: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:升序?
總數(shù):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
一張表,一旦分組完成后,查詢后只能得到組相關(guān)的信息。?
組相關(guān)的信息:(統(tǒng)計信息)?count,sum,max,min,avg 分組的標(biāo)準(zhǔn))
select * from table1 where time between time1 and time2?
select a,b,c, from table1 where a not between 數(shù)值1 and 數(shù)值2
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
?
轉(zhuǎn)載于:https://www.cnblogs.com/Auraro/p/7217928.html
總結(jié)
- 上一篇: 正则表达式里转义字符_五分钟搞定正则表达
- 下一篇: 天龙八部网单服务器技能修改,天龙八部3门