標籤:sql教程
一、SQL教程
1、SQL文法
DML
select查詢資料
update更新資料
delete刪除資料
insert into插入資料
DDL
create database建立資料庫表
alter database修改資料庫
create table建立表
alter table修改表
drop table刪除表
create index建立索引
drop index刪除索引
set names utf8;設定使用的字元集
SQL語句對大小寫不敏感。
2、SQL SELECT
select * from 表名
select 列名 from 表名
select lastname,firstname from persons;
3、SQL SELECT DISTINCT
select distinct column_name,column_name from table_name;
select distinct country from websites;
4、SQL WHERE
select column_name,column_name from table_name where column_name operator value;
select * from websites where country=‘cn‘
SQL使用單引號(‘‘)來圍繞文本值
如果數字欄位,請不要使用單引號。
select * from websites where id=1
下面的運算子可以在where語句中使用:
= 等於
<> 不等於(在sql的一些版本中也可以用!=)
> 大於
< 小於
>= 大於等於
<= 小於等於
between 在某個範圍內
like 搜尋某個模式
in 制定針對某列的多個可能值
5、SQL ADN & OR
如果第一個條件和第二個條件都成立,則 AND 運算子顯示一條記錄。
如果第一個條件和第二個條件中只要有一個成立,則 OR 運算子顯示一條記錄。
select * from websites where country=‘cn‘ and alexa>50
select * from websites where country =‘usa‘ or country=‘cn‘
select * from websites where alexa > 50 and (country=‘cn‘ or country=‘usa‘)
6、SQL ORDER BY
ORDER BY 關鍵字用於對結果集按照一個列或者多個列進行排序。
ORDER BY 關鍵字預設按照升序對記錄進行排序。如果需要按照降序對記錄進行排序,您可以使用 DESC 關鍵字。
select column_name,column_name from table_name order by column_name,column_name asc|desc
select * from websites order by alexa
select * from websites order by alexa desc
select * from websites order by country,alexa
7、SQL INSERT INTO
INSERT INTO 語句用於向表中插入新記錄。
insert into table_name values(v1,v2,v3,.....);
insert into table_name(c1,c2) values(v1,v2);
insert into websites(name,url,alexa,country) values(‘百度‘,‘https://www.baidu.com‘,4,‘cn‘)
id列是自動更新的
insert into websites(name,url,country)values (‘stackoverflow‘,‘http:/stackoverflow.com‘,‘ind‘)
8、SQL UPDATE
update table_name set c1 = v1,c2 = v2 where c3 = v3;
WHERE 子句規定哪條記錄或者哪些記錄需要更新。如果您省略了 WHERE 子句,所有的記錄都將被更新!
update websites set alexa =5000,country=‘usa‘ where name=‘菜鳥教程‘
9、SQL DELETE
delete from table_name where c1 = v1;
WHERE 子句規定哪條記錄或者哪些記錄需要刪除。如果您省略了 WHERE 子句,所有的記錄都將被刪除!
delete from websites where name=‘百度‘ and country = ‘cn‘
刪除所有記錄
DELETE FROM table_name;
或
DELETE * FROM table_name;
本文出自 “莊勇的個人部落格” 部落格,請務必保留此出處http://zhuangyong.blog.51cto.com/12459964/1887175
一、個人總結的菜鳥教程|sql教程的知識點(SQL教程)