常用SQL语句总结

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用SQL语句总结相关的知识,希望对你有一定的参考价值。

常用SQL语句总结

常用SQL语句总结:

创建数据库:CREATE DATABASE database-name

删除数据库:drop database dbname

创建数据表:create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

删除表:drop table tabname 

为表增加数据列:Alter table tabname add column col type

为表添加主键:Alter table tabname add primary key(col) 

删除数据表主键: Alter table tabname drop primary key(col)

为数据表创建索引:create [unique] index idxname on tabname(col….)

删除数据表索引:drop index idxname

为数据表创建视图:create view viewname as select statement

删除数据表的视图: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]
总数: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

左连接:

SELECT Developers.Name, Customer_orders.ID FROM Developers LEFT JOIN Customer_orders ON Developers.ID = Customer_orders.customer_id ORDER BY Developers.Name;

右连接:

SELECT Customer_orders.ID, Employees.Last_name, Employees.First_name FROM Customer_orders RIGHT JOIN Employees ON Customer_orders.employee_id = Employees.ID ORDER BY Customer_orders.ID;

全连接:

SELECT Customers.Name, Customer_orders.ID FROM Customers FULL OUTER JOIN Orders ON Customers.ID=Customer_orders.customer_id ORDER BY Customers.Name;

like匹配:SELECT * FROM users WHERE email LIKE '%gmail%';

排序(order):SELECT * FROM users ORDER BY email DESC;

唯一值(distinct):SELECT DISTINCT City FROM Developers;

数据分组(group by): SELECT COUNT(ID), City FROM Developers GROUP BY City;

存在检查(exists):SELECT id, name FROM customers WHERE EXISTS (SELECT id FROM customer_orders WHERE customer_orders.customer_id = customers.id AND customers.city = "Rome");

参考:Most Used SQL Queries For Beginner

参考:sql常用语句汇总

以上是关于常用SQL语句总结的主要内容,如果未能解决你的问题,请参考以下文章

常用SQL语句总结

常用的sql语句总结

sql常用 语句总结

常用SQL语句大全总结

常用SQL语句大全总结

常用SQL语句总结