后面发现SQL语句其实不是很难,一天就能上手。(小白表示不相信++
纯干货笔记,若阅读起来稍有障碍(或许只有我看得懂),请直接拉到后文查看大佬链接/?
SQL高级教程
TOP
语法:select top 2 * from table_name -> 选取头两行
select top 50 percent * from table_name -> 从头开始取百分之五十的数据
1-> Oracle不支持
LIKE
语法:select col_names from table_name where col_name1 like n%(%n,%n%)
1-> 选取col_name1的值以n开头(以n结尾,含有n的)的所有行
通配符
%–> 替代一个和多个字符
_–> 只可替代一个字符
[charlist]–> 字符列中任何单一字符
[^charlist]或者[!charlist]–> 不再字符列中的任何单一字符
JOIN
语法:
select col_names from
table_name1 inner join table_name2
where contidion
1-> 用于根据两个表或多个表之间的列的关系,从表中查询这些数据
分别为inner,full,left,right,我认为分别是交集,并集,以左边的为主,以右边的为主
UNION
语法:
Select columnsName from tableName1
Union (all)
Select columnsName from tableName2
1-> UNION 操作符用于合并两个或多个 SELECT 语句的结果集。.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
2-> union all 表示允许重复值
SELECT INTO
语法:select col_names into new_table from old_table
1-> 原理其实就是select语句,只不过多加了一步将选取的结果存入新表中
CREATE DATABASE
语法:create database db_name
CREATE TABLE
语法:create table table_name(col1 datatype,col2 datatype…)
CONSTRAINT
六种约束,分别是UNIQUE,PRIMARY KEY,FOREIGN KEY,CHECK,DEFAULT,NOT NULL。前四种的代码格式类似。
unique
当创建表时
create table tableName(col1 datatype unique,…)
若要为命名约束或多列定义unique约束则
create table tableName(
col1 datatype,
col2,datatype,…
CONSTRAINT uc_PersonID UNIQUE (col1,col2)
)
当表已创建,当创建列时约束
alter table tableName add unique(colName)
当表已创建,要命名约束和多列定义时
alter table tableName
add constraint uc_PersonID unique(col1,col2)
撤销unique约束:
alter table tableName
drop constraint uc_PersonID
primary key
当创建表时
create table tableName (col1 datatype primary key,…)
若要为命名约束或多列定义primary key约束则
create table tableName(
col1 datatype,
col2,datatype,…
CONSTRAINT pk_PersonID,primary key(col1,col2)
)
当表已创建,当创建列时约束
alter table tableName add primary key(colName)
当表已创建,要命名约束和多列定义时
alter table tableName
add constraint pk_PersonID primary key (col1,col2)
撤销primary key约束:
alter table tableName
drop constraint pk_PersonID
foreign key
当创建表时
create table tableName (col1 datatype foreign key,…)
若要为命名约束或多列定义foreign key约束则
create table tableName(
col1 datatype,
col2,datatype,…
CONSTRAINT fk_PerOrders foreign key(col1,col2)
)
当表已创建,当创建列时约束
alter table tableName add foreign key (colName)
当表已创建,要命名约束和多列定义时
alter table tableName
add constraint fk_PerOrders foreign key(col1,col2)
撤销foreign key约束:
alter table tableName
drop constraint fk_PerOrders
PS:该表的外键一定是另外一个表的主键
check
当创建表时
create table tableName (col1 datatype check,…)
若要为命名约束或多列定义foreign key约束则
create table tableName(
col1 datatype,
col2,datatype,…
CONSTRAINT chk_Person check(col1,col2)
)
当表已创建,当创建列时约束
alter table tableName add check (colName)
当表已创建,要命名约束和多列定义时
alter table tableName
add constraint chk_Person check(col1,col2)
撤销check约束:
alter table tableName
drop constraint chk_Person
default
Create table tableName(col1 datatype default value……)
如果表已经存在,则
Alter table tableName
alter column columnsName set default values
撤销约束
Alter table tableName
alter column columnsName drop default
PS:默认约束,如果没有规定值,则填入默认值
not null
create table tableName(col1 datatype not null,…)
强制不接受NULL值
CREATE INDEX
语法:create (unique) index index_name on table_name (col_name)
1-> 相当于pandas的set_index,将表中的一列或多列变为整个表的(多层)索引
2-> unique 是指任意两行一定不能有相同的索引
DROP
语法:drop index index_name,drop table tableName,drop database dbName -> 撤销索引,表及数据库
1-> truncate table tableName,仅删除表中的数据而不删除结构
ALTER TABLE
语法:
alter table table_name
add column_name datatype//drop column column_name//alter column column_name datatype ->在已有表中添加,删除和更新列(的数据类型)
AUTO INCREMENT
Auto-increment 会在新记录插入表中时生成一个唯一的数字
通过 sequence 对创建 auto-increment 字段(该对象生成数字序列)
Create sequence seq_increment
Minvalue 1
Startwith 1
Increment by 1
Cache10
上面的代码创建名为 seq_person 的序列对象,它以 1 起始且以 1 递增。该对象缓存 10 个值以提高性能。CACHE 选项规定了为了提高访问速度要存储多少个序列值。要在 “Persons” 表中插入新记录,我们必须使用 nextval 函数(该函数从 seq_person 序列中取回下一个值)
Insert into table_name (id,col1,col2) values (seq_person.nextval,one,two)
VIEW
视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。我们可以向视图添加 SQL 函数、WHERE 以及 JOIN 语句,我们也可以提交数据,就像这些来自于某个单一的表。注释:数据库的设计和结构不会受到视图中的函数、where 或 join 语句的影响。
语法:view_name 像 [current product list]一样
#创建视图
create view view_name as
select col_name
from table_name
where condition
#查询视图
select * from view_name (where…)
#更新视图
create or replace view_name as
select co1_name,co2_name…
from table_name
where condition
#撤销视图
drop view view_name
NULL
NULL 与 0 是不等价的
在Oracle中,若想要null值用0代替,则NVL(colName,0),colName中的所有NULL值将会用0进行计算
大佬指导书—>戳一戳