08.create database:创建数据库
例:创建一个叫my_db 的数据库
create database my_db
09.create table:创建数据库中的表
9.1创建一个叫persons的表,包含五列
列名分别是id lastname fristname address city
create table persons(
id int,
lastname varchar(255), //表示最大长度为255个字符
firstname varchar(255),
address varchar(255),
city varchar(255),
)
9.2向创建的表格添加非空约束 not null
create table persons(
id int not null, //强制该列不接受空值
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255),
)
9.3添加唯一约束 unique constrains
create table persons(
id int unique, //每一行的id都是唯一
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255),
)
//为多个列定义unique约束
//为多个列定义unique约束
create table persons(
id int not null,
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255),
constraint uc_persons unique(id,lastname) //必须给约束命名,此处命名为
//uc_persons(unique constraint persons)
)
ps.当表已经创建时
//给某列添加约束
alter table persons add unique (id)
//给多列添加约束
alter table persons add constraint uc_persons unique(id,lastname) //添加约束
//撤销unique约束
alter table persons drop constraint uc_persons//删除约束
9.4添加primary key 约束:
ps:1.一个表只能有一个PRIMARY KEY,但可以有多个UNIQUE KEY
2.primary key = unique + not null
create table persons(
id int unique primary key, //每一行的id都是唯一,而且不能不写(为空)
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255),
)
1.为多列定义primary key
create table persons(
id int unique primary key,
lastname varchar(255),
firstname varchar(255),
address varchar(255),
city varchar(255),
constriant pk_persons primary key (id,lastname)
)
2.已存在表的情况下为id创建primary key
alter table persons add primary key (id),
3.已存在表的情况下为多列创建primary key
alter table persons add constraint pk_persons primary key (id,lastname) //也就是加个名字的区别
4.撤销primary key
alter table persons drop constriant pk_persons,
9.5foreign key 约束:
一个表中的foreign key 指向另一个表中的primary key
预防破坏表之间连接的动作
防止非法数据插入外键列
例: 在persons表中primary key 为id_p
在orders表中 primary key 为id_o
1.在 orders中为id_p创建foreign key
create table orders(
id_o int not null primary key,
orderno int not full,
id_p int foreign key references persons(id_p)
)
2.为多列定义foreign key
create table orders(
id_o int not null,
orderno int not full,
id_p int ,
primary key(id_o),
constraint fk_PerOders foreign key (id_p) references persons(id_p) //为多列创建
)
3.表存在的情况下创建foreign key
alter table orders
add foreign key(id_P)
references persons (id_p)
4.表存在的情况下创建多个foreign key
alter table orders
add constriant fk_PerOrders foreign key (id_p) references persons(id_p)
5.撤销foreign key
alter table orders
drop constraint fk_PerOders
9.6 check约束:只允许写入固定的值
1.创建check约束
create table persons(
id int not full check (id>0) ,
lastname varchar(255) ,
)
2.为多个列定义check约束
create talbe persons(
id int not full,
city varchar(255)
constriant chk_persons check(id>0 and city='Room')
)
3.表已存在创建check约束
alter table persons
add check(id>0),
4.为多个列创建check 约束
alter table persons
add constraint chk_persons
check(id>0 and city='Room')
5.撤销check约束
alter table persons
drop constraint chk_persons
9.7default约束
作用:向列中添加默认值,如果没有规定其他值,就把默认值写入
1.
create table persons(
id int not full,
name varchar(255) ,
city varchar(255) default 'Room'
)
2.表已经存在的情况下创建defualt约束
alter table persons
alter columu city
set defualt 'Room',
3.撤销defualt约束
alter table persons
alter columu city
drop defualt
10.create index :创建索引
例:创建一个叫PersonIndex的索引,在person表的lastname列
create index PersonIndex on person(lastname),
如果是降序索引:
create index PersonIndex on person (lastname desc),
如果不止一个列
create index PersonIndex on person(lastname,fristname)
11.drop与alter
1.删除表格中的索引
drop index persons.PersnoIndex
2.删除表
drop table persons
3.删除数据库
drop database person.db,
4.删除表中数据,而不删除表本身
truncate table persons,
5.在表中添加一个叫brithday的新列
alter table persons
add birthday date //表示新建列的类型时date,用来存放日期
6.将brithday的数据类型改为year
alter table persons
alter columu birthday year
7.删除birthday列
alter tabel persons
drop columu brithday
12.identity
create table persons(
id int primary key identity,
name varchar(255),
city varchar(255),
)
默认让id从1开始,使每条记录递增1
ps.若要让id从20开始,每条递增10,则写为identity(20,10)
13.view:创建可视化的表
例一:创建并查询视图
创建一个叫 Current Product List的视图,从products表内拿出
价格高于平均价格的产品
的id与名称
create view[Current Product List] as
select id, name from products
where price>(select avg(price) from Products)
select * from [Current Product List]
//也可以添加查询条件
select * from [Current Product List] where year =1997
//若为视图新添Catagory这一列
create view[Current Product List] as
select id, name,catagory from products //直接加就行
where price>(select avg(price) from Products)
例二:从一个叫[product sales for 1997]的表中
计算每个种类的销售总数
并创建名叫[category sales for 1997]的视图展示
create view [catagory sales for 1997] as
select distinct CategoryName,Sum(ProductSales) as CategorySales
from[product sales for 1997]
order by CategoryName
例三:删除视图
drop view [product sales for 1997]
14.DATE函数
getdate() | 返回当前日期和时间 |
datepart() | 返回日期/时间的单独部分 |
dateadd() | 在日期中加减指定的时间间隔 |
datediff() | 返回两个日期之间的时间 |
convert() | 转换时间格式 |
常见的格式:
date-- YYYY-MM-DD
datetime-- YYYY-MM-DD HH:MM:SS
smalldatetime 同上,但是精确度更小
timestamp -- 唯一的数字
ps.得到的结果必须含有完整的时间格式
select * from orders where date='1999-11-20'