SQL(3)

来源:互联网 发布:软件性能 编辑:程序博客网 时间:2024/06/16 12:38

AS
可以为表名或列名指定别名
select name as n,country as c from websites; 为列名指定别名
select name ,concat(url,‘,’,alexa,‘,’,country)as site_info from websites; 将url alexa country 结合在一起,并创建一个名为site_info的别名。
select w.name,w.url from websites as w where w.name = “”; 为表创建别名。

JOIN
用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段
select websites.id ,websites.name,access_log.count,access_log.date
from websites inner join access_log on websites.id = access_log.site_id;
inner join 关键字在表中至少一个匹配时返回行.
left join关键字从坐标返回所有的行,即使右标没有匹配.如果右表没有匹配,则返回null.
right join 关键字从右表返回所有的行,即使左表没有匹配.
full outer join 只要左表和右表其中一个表存在匹配,则返回行.

UNION
合并两个或多个select语句的结果
select neme from table1
union
select name from table2;
如果允许重复值,使用 union all。

SELECT INTO 从一个表复制数据,然后把数据插入到另一个新表中。
select *
into newtable
from table1;
实例:
select *
into websitesBackup2016
from websites; 创建websites的备份附件

select name,url
into websitesBackup2016
from websites
where country=“cn”

insert into select从一个表复制数据,然后把数据插入到一个已存在的表中
insert into table2
select * from table1;

insert into websites (name,country)
select app_name,country from apps
where id=1;

create database dbname;创建数据库
create table tablename;创建数据库表
create table table_name
(
Column_name1 data_type(size),
Column_name2 data_type(size),

)
Column_name为表中列的名称。
Data_type规定列的数据类型
Size规定表中列的最大长度

SQL约束:规定表中的数据规则
not null约束
create table persons

P_id int not null,
Lastname varchar(255) not null, 不接受null值
Firstname varchar(255),

unique约束
创建表时,创建unique约束
在MySQL中
CREATE TABLE Persons

P_id int not null,

unique(p_id)

SQL Server
Create table persons
(
P_id int not null unique,

)

命名unique约束,并定义多个列的unique约束
create table persons

P_id int not null,
lastName varchar(255)not null

constraint uc_personId unique(p_id,lastName)

alter table时的sql unique约束
alter table persons
add unique(p_id);

alter table persons
add constraint uc_personid unique (p_id,lastName)

撤销约束
MySQL:
alter table persons
drop index uc_personId

SQL Server
alter table persons
drop constraint uc_personId

primary key约束
主键(唯一标识数据库表中的每条记录,包含唯一的值,不能包含null值,每个表有且只有一个主键)

foreign key 约束
外键,一个表中的foreign key 指向另一个表中的primary key
foreign key(p_id)references persons(P_id)
P_id int foreign key references persons(p_id)

check约束
Check约束用于限制列中的值得范围

default约束
用于向列中插入默认值,如果没有规定其他的值,会将默认值添加到所有的新纪录。
create table persons
(
P_id int not null;
City varchar(255) default‘Sandnes’
)

alter table 时的SQL DEFAULT约束
alter table persons
alter city set default’sandnes’
撤销约束:
alter table persons
alter city drop default

create index用于在表中创建索引
create index index_name on table_name (column_name)简单索引,允许使用重复的值
create unique index index_name on table_name(column_name)创建唯一索引,不允许使用重复的值;
实例:
create index pIndex on Persons(LastName)
create index pIndex on Person(LastName,FirstName)索引不止一个列

Drop可用于删除索引、表、数据库
alter table table_name drop index index_name MySQL中删除索引
drop table table_name;
drop database database_name;
truncate table table_name;仅删除表内数据,不删除表本身

Alter table用于在已有的表中添加、删除和修改列。
表中添加列:
alter table table_name
add column_name datatype;
表中删除列:
alter table table_name
drop column column_name;

Auto increment在新纪录插入表中时生成一个唯一的数字
MySQL中:
Id int not null auto_increment
Access中
Id integer primary key autoincrement

视图:
创建视图
create view view_name as
select column_name
from table_name
where condition

撤销视图:
drop view view_name;

原创粉丝点击