SQL操作

创建表

语法 create table 表名(列+类型,列+类型);

create table Websites (

id int,

name varchar(255),

url varchar(255),

alexa int,

country varchar(255)

)

插入

语法 insert into 表名(列名,列名) values(内容,内容);

insert into Websites(id,name,url,alexa,country) values(1,'Google','https://www.google.cm/',1,'USA');

insert into Websites(id,name,url,alexa,country) values(2,'淘宝',' https://www.taobao.com/',13,'CN');

insert into Websites(id,name,url,alexa,country) values(3,'菜鸟教程',' http://www.runoob.com/',4689,'CN');

insert into Websites(id,name,url,alexa,country) values(4,'微博','http://weibo.com/',20,'CN');

insert into Websites(id,name,url,alexa,country) values(5,'FaceBook','https://www.facebook.com/',3,'USA');

删除

DELETE FROM 表名称 WHERE 列名称 = 值

delete form websites where id = 1

更新修改

语法 UPDATE 表名称 SET 列名称 = 新值 ,列名称 = 新值 WHERE 列名称 = 某值

UPDATE websites SET id = 3 WHERE NAME ='菜鸟教程'

查询

语法 select * from 表名 limit 2, 1 或 limit 2 offset 1

select * from websites where id >3 limit 2

排序

order by 升序

order by 列名,列名 desc 降序

select id,name,url,alexa,country from websites order by alexa desc

去重

语法 select distinct 列名称 from 表名称

select distinct * from websites

高级

top 取前N个数据

mysql limit

语法 select * from 表名 limit 3

sqlserver

语法 select top 5 * from 表名

select top 50 percent * from 表名

like 模糊查询

语法 select * from 表名 where 列名 like '%00%'

like '李*'

like '李%'

like '%[0-9]%'

like ‘%[a-z]%’

like '%[!0-9]%'

select * from table1 where name like ‘%明%’

通配符

%代替 0个或者多个字符

_代替 1个字符

[charlist] 字符列中的任何单一字符

[^charlist]或 [!charlist] 不在字符列中的任何单一字符

语句

SELECT * FROM Websites WHERE name REGEXP '^[GFs]';

SELECT * FROM Websites WHERE name REGEXP '^[^A-H]';

In 操作符

IN 操作符允许您在 WHERE 子句中规定多个值。

语法: select * from 表名 where 列名 in (value1,value2,value3) ;

SELECT * FROM websites WHERE NAME IN ('Google','facebook');

between

语法: SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;

语句

下面的 SQL 语句选取 alexa 介于 1 和 20 之间的所有网站:

SELECT * FROM Websites WHERE alexa BETWEEN 1 AND 20;

NOT BETWEEN 操作符实例

SELECT * FROM Websites WHERE alexa NOT BETWEEN 1 AND 20;

带有 IN 的 BETWEEN 操作符实例

下面的 SQL 语句选取alexa介于 1 和 20 之间但 country 不为 USA 和 IND 的所有网站:

SELECT * FROM websites WHERE(alexa NOT BETWEEN 3 AND 13 ) AND NOT country IN ('cn')

带有文本值的 BETWEEN 操作符实例

下面的 SQL 语句选取 name 以介于 'A' 和 'H' 之间字母开始的所有网站:

SELECT * FROM Websites WHERE name BETWEEN 'A' AND 'H';

带有日期值的 BETWEEN 操作符实例

下面的 SQL 语句选取 date 介于 '2016-05-10' 和 '2016-05-14' 之间的所有访问记录:

SELECT * FROM access_log WHERE date BETWEEN '2016-05-10' AND '2016-05-14';

别名

语法 SELECT column_name(s) FROM table_name AS alias_name;

Join

SQL join 用于把来自两个或多个表的行结合起来。

SELECT websites.id ,websites.name AS '名字',websites.url,websites.country,access.datet,access.count FROM websites JOIN access ON websites.id = access.id

约束

PCREATE TABLE Persons(Id_P int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255),PRIMARY KEY (Id_P))

外键

一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY。

"Persons" 表:

Id_P

LastName

FirstName

Address

City

1AdamsJohnOxford StreetLondon

2BushGeorgeFifth AvenueNew York

3CarterThomasChangan StreetBeijing

"Orders" 表:

Id_O

OrderNo

Id_P

1778953

2446783

3224561

4245621

FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)

你可能感兴趣的:(SQL操作)