PostgreSQL9.4
定义:对象-关系型数据库管理系统 ,PostgreSQL支持大部分SQL标准并且提供了许多其他现代特性:复杂查询、外键、触发器、视图、事务完整性、MVCC
DDL
定义:数据定义语言,主要对库、表进行操作
1、创建数据库
create database 库名;
2、删除数据库
drop databases 库名;
3、修改数据库名
alter database 源库名 rename to 新库名;
4、创建表
create table 表名 {
字段 类型 约束(主键约束、非空约束...)
.......
字段 类型
}
案例
create table tb_dept2
(
id int primary key,
name varchar(22),
location varchar(50)
);
5、删除表
(I、删除无关联表)
drop table 表名;
(II、删除关联表)
案例:创建一个和tb_dept2有关联的表
create table tb_emp
(
id int primary key,
name varchar(25),
deptId int,
salary float,
constraint fk_emp_dept foreign key (deptId) references tb_dept2(id)
);
首先需要删除外键约束再进行删除表
--1.接触外键约束
alter table tb_temp drop constraint fk_emp_dept;
--2.删除表
drop table tb_dept2;
6、修改表
--1. 修改表名
alter table 源表名 rename to 新表名;
--2. 修改表字段名
alter table tb_dept2 rename name to names;
--3. 修改表字段类型
alter table 表名 alter column 字段名 type 类型;
DML
定义:数据操作语言,用于添加、修改和删除表中的行
1、添加数据
-- 1.插入单条数据
insert into 表名 (字段1,字段2...) values (值1,值2...);
-- 2.插入多条数据
insert into 表名 (字段1,字段2...)
values (值1,值2...),
(值1,值2...)
...
;
-- 3. 从另一张表中插入数据(将tb_dept2表中数据插入到person表中)
insert into person (id,name,age) select id,name,age from tb_dept2;
2、更新数据
update 表名 set 字段=? 条件
-- 案例(将person中年龄10-20的姓名全改为zs)
update person set name='zs' where age between 10 and 20;
3、删除数据
delete from 表名 条件
-- 案例
delete from person where age between 10 and 20;