本文使用的是MySQL,内容为学习SQL语句期间遇到的问题和注意事项。
参考MySQL菜鸟教程
MySQL 与 [charlist]% 通配符
按照提示使用如下语句匹配不到结果
select * from persons where city like '[ALN]%'这种语法在MySQL中不适用,必须以正则表达式代替,正确语句如下(regexp或者rlike关键字均可)
select * from persons where city rlike '^[ALN]%' select * from persons where city regexp '^[ALN]%'如果需要区分大小写,使用binary关键字
select * from persons where city rlike binary '^[ALN]%'需要的是非A,非N,非L开头的数据,在关键字前加入 ^
select * from persons where city rlike '^[^ALN]%'
转载于:https://www.cnblogs.com/cherrysu/p/8023371.html
MySQL between 用法
where column between value1 and value2
where column not between value1 and value2MySQL between 边界
- 在 MySQL 中,BETWEEN 包含了 value1 和 value2 边界值有的数据库则不包含 value1 和 value2 边界值(类似于 > and <),也有数据库包含 value1 而不包含 value2(类似于 >= and <).
MySQL between 时间日期
// DATE 格式,查询 2008-08-08 到 2009-01-01 零点之前的数据
SELECT * FROM table WHERE column_time BETWEEN ‘2008-08-08’ AND ‘2009-01-01’// DATETIME 格式,查询 2008-08-08 20:00:00 到 2009-01-01 零点之前的数据
SELECT * FROM table WHERE column_time BETWEEN ‘2008-08-08 20:00:00’ AND ‘2008-12-31 23:59:59’
MySQL between 数据比较
expr BETWEEN min AND max当 expr 表达式的值大于或等于 min 且小于或等于 max 时, BETWEEN 的返回值为 1 ,否则返回 0 。利用这个功能,可以判断一个表达式或值否则在某个区间
MySQL union 用法
- 用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 select 语句会删除重复的数据。
- MySQL UNION 操作符语法格式:
select expression1, expression2, ... expression_n from tables [where conditions] union [all] select expression1, expression2, ... expression_n from tables [where conditions] - expression1, expression2, … expression_n: 要检索的列。
tables: 要检索的数据表。
where conditions: 可选, 检索条件。
all: 可选,返回所有结果集,包含重复数据。
- MySQL UNION 操作符语法格式:
MySQL insert into select 语句
- insert into select 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。
- 我们可以从一个表中复制所有的列插入到另一个已存在的表中(两个表结构一样):
insert into table2 select * from table1 - 或者我们可以只复制希望的列插入到另一个已存在的表中(两个表结构不一样):
insert into table2(column_name(s)) select column_name(s) from table1
- 我们可以从一个表中复制所有的列插入到另一个已存在的表中(两个表结构一样):
MySQL 数据类型
MySQL unique 约束
UNIQUE 约束唯一标识数据库表中的每条记录,每个表可以有多个 unique 约束,但是每个表只能有一个 primary key 约束。
- 创建约束:
create table Persons ( Id_P int not null, LastName varchar(255) not null, unique (Id_P) ) - 命名 unique 约束,以及为多个列定义 unique 约束:
create table Persons ( Id_P int not null, LastName varchar(255) not null, constraint unique_name unique (Id_P, LastName) ) - 表已被创建时,在 “Id_P” 列创建 UNIQUE 约束:
alter table Persons add unique (Id_P) alter table Persons add constraint unique_name unique (Id_P,LastName) - 撤销 UNIQUE 约束:
alter table Persons drop index uc_PersonID
MySQL 主键约束(primary key)
- 表设计时一定有主键,表中的某个字段添加主键约束后,该字段为主键字段,主键字段中出现的每一个数据都称为主键值,即使表中两行记录相关数据相同,但由于主键值不同,所以也认为是两行不同的记录。
- 单一主键(列级定义):
create table Persons ( Id_P int primary key, LastName varchar(255) not null, ) - 单一主键(表级定义):
create table Persons ( Id_P int, LastName varchar(255) not null, constraint pk_name primary key(Id_P) ) - 复合主键(表级定义)
create table Persons ( Id_P int, LastName varchar(255) not null, constraint pk_name primary key(Id_P, LastName) ) - 自动生成主键值,自增数从1开始,以1递增:
create table Persons ( Id_P int primary key auto_increment, LastName varchar(255) not null, ) - 表已存在的情况下为 “Id_P” 列创建 PRIMARY KEY 约束:
alter table Persons add primary key (Id_P) - 撤销 PRIMARY KEY 约束:
alter table Persons drop primary key
MySQL 外键约束(foreign key)
- 若有两个表A、B,id是A的主键,而B中也有id字段,则id就是表B的外键,外键约束主要用来维护两个表之间数据的一致性。某个字段添加外键约束之后,该字段称为外键字段,外键字段中每个数据都是外键值。
- 一张表可以有多个外键字段(与主键不同)
- 创建表时创建外键:
create table Orders ( Id_O int not null, OrderNo int not null, Id_P int, primary key (Id_O), foreign key (Id_P) references Persons(Id_P) ) - 命名 foreign key 约束,以及为多个列定义 foreign key 约束
create table Orders ( Id_O int not null, OrderNo int not null, Id_P int, primary key (Id_O), constraint fk_name foreign key (Id_P) references Persons(Id_P) ) - 在 “Orders” 表已存在的情况下为 “Id_P” 列创建 foreign key 约束:
alter table Orders add foreign key (Id_P) references Persons(Id_P) - 撤销 foreign key 约束:
alter table Orders drop foreign key fk_name
MySQL check 约束
- 未测试
MySQL default 约束
- 在 “Persons” 表创建时为 “City” 列创建 DEFAULT 约束:
create table Persons ( Id_P int not null, LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255) default 'Sandnes' ) - 在表已存在的情况下为 “City” 列创建 default 约束:
alter table Persons alter column City set default 'Sandnes' - 撤销 DEFAULT 约束:
alter table Persons alter City drop default
