表
来源:互联网 发布:知乎怎样查航班动态 编辑:程序博客网 时间:2024/06/08 12:42
表的约束:
- 是加在表上的一种对象,能保证每次插入或修改数据的时候,对数据的合法性进行检查,从而避免插入不合理数据。
- 优点:通过检查,能保证数据的完整性和一致性。
- 缺点:消耗一定的存储,数据量大的时候,每次检查会有一定的资源损耗
-- 首先,创建表:类型、长度、非空create table ddl_test1 ( id int, name varchar2(20) not null, birthday date, sex int);-- 在表 ddl_test1 上增加一个名字为 fk_ddl_sex_01 的约束-- 作用在 sex 列上 / 关联到 ddl_sex 表的 id 列alter table ddl_test1 add constraint fk_ddl_sex_01 foreign key (sex) references ddl_sex (id);create table ddl_sex ( id int, value varchar2(10));alter table ddl_sex add constraint pk_ddl_sex primary key(id);select * from ddl_sex;---- 创建约束的几种方式---- 第一种方式,先创建表,再初始化数据,最后再加约束-- 创建表create table ddl_test2 ( id int, name varchar2(20) not null, birthday date, sex int);create table ddl_sex2 ( id int, value varchar2(10));-- 插入数据insert into ddl_sex values (1, '男');insert into ddl_sex values (2, '女');insert into ddl_test2 values (2, 'xx', sysdate, 1);insert into ddl_sex values (3, '不知');insert into ddl_test2 values (1, 'xx', sysdate, 1);insert into ddl_test2 values (3, 'xx', sysdate, 1);insert into ddl_test2 values (4, 'xx', sysdate, 1);-- 增加约束alter table ddl_test2 add constraint fk_0023 foreign key (sex) references ddl_sex;alter table ddl_test2 add constraint pk_test2 primary key (id);---- 第二种方式,在建表的字段上直接建立约束create table ddl_test4( id int primary key, name varchar2(20) not null, sex constraint hello250 references ddl_sex);---- 第三种方式,将建立约束的语句,放到建表语句最后。优点,清晰易于管理。create table ddl_test5( id int, name varchar2(20) not null, sex int, constraint hello260 primary key(id), foreign key (sex) references ddl_sex );---- 约束的种类---- 主要有:主键约束、非空约束、唯一约束、检查约束、外键约束等create table ddl_test6 ( id int, sal number(5));alter table ddl_test6 add constraint pk_test6 primary key (id);alter table ddl_test6 add constraint ck_2323 check(sal > 1250);insert into ddl_test6 values (1, 500);insert into ddl_test6 values (2, 3500);select * from ddl_test6;
表的命名:
- 简
- 明
另外:
- 表名等在数据库内部会自动转化为大写的形式。如果想使用小写的形式,需要在创建的时候,加双引号。
create table "lowcase_name" (...);
- 表名尽量使用英文单词,或英文单词缩写词。如果有多个单词连接,请使用下划线。
- 不要使用复数形式。比如使用
boy
而不是boys
, 使用student
而非students
. - 字段名字有两种方式:
-- 清晰明了不拖泥带水,但多表联合查询,可能出现重复字段create table boy (id int, name varchar2(20), wechat varchar2(20));-- 写法丑,但联合查询不会出现重复字段create table girl (girl_id int, girl_name varchar2(20), girl_wechat varchar2(20));
- 主键请使用 代理主键, 即没有任何业务关联的字段作为主键。因为直觉上不变的东西,在特定情况下都可能发生变化。
- 自增,请使用 序列, 最好为每个单独的主键创建一个专用的序列。
-- 首先,保证有创建序列的权限GRANT CREATE SEQUENCE TO VIP;-- 创建序列的最简语句-- 注意,命名中,最好带 seq 等字段,表示这是一个序列CREATE SEQUENCE seq_boy;-- 使用的方式很简单insert into boy values (seq_boy.nextval, 'xxx');select seq_boy.currval -- 序列当前值 seq_boy.nextval -- 序列下一个值 from dual;-- 序列可以有更多参数create sequence seq_boy2 minvalue 2 -- 最小值,默认 1 maxvalue 1000 -- 最大值,默认无限 start with 4 -- 初始值,默认跟 minvalue 相同 increment by 2 -- 步进 nocycle -- 如果到达最大值,是否从开始再次循环 nocache -- 设置缓存 ;-- 修改alter sequence seq_boy2 cache 10;alter sequence seq_boy2 maxvalue 2000 increment by 5;-- 删除drop sequence seq_boy2;
修改, ALTER:
-- 创建示例表create table ddl_test1( id int primary key, name varchar(2) not null, birth date default sysdate);-- 各种修改alter table ddl_test1 rename column id to testid;alter table ddl_test1 modify (name varchar2(40), birth timestamp);alter table ddl_test1 add gender varchar2(10) not null;alter table ddl_test1 drop column gender;-- 作用在序列上的 DDLcreate sequence seq_aaa;alter sequence seq_aaa start with 222;drop sequence seq_bbb;
查询, select:
-- SELECT 语句中的基本计算,是基于行的-- 不同行中的数据,不能直接计算-- 所以,解决的方案是:将不同行的数据整合到同一行中,再做比较-- 而整合到同一行,有很多方法:-- 可以是表跟表进行关联;可以是分组;等等。-- 关键是:思路,思路,思路。-- 下面是一个例子-- 查询男、女之差-- 首先,初始化数据-- 男 / 46人-- 女 / 10人create table aaa (gender char(2), num number);insert into aaa values ('男', 46);insert into aaa values ('女', 10);-- 1. 这是一种方法,with a as (select * from aaa where gender = '男'), b as (select * from aaa where gender = '女')select a.num - b.num from a, b;-- 2. 下面是上面方式的等价写法select a.num - b.num from (select * from aaa where gender = '男') a, (select * from aaa where gender = '女') b;-- 3. 下面是另一种写法select (select num from aaa where gender = '男') - num from aaa where gender = '女';-- 4. 最基本的,对表进行关联查询,之后再过滤,再计算select a.num - b.num from aaa a, aaa b where a.gender='男' and b.gender='女';-- 5. 或者基于分组select max(num) - min(num) from aaa;-- 6.7.8. 其他方法...
阅读全文
0 0
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- 表
- [BZOJ1076]奖励关-状压+期望
- 全面理解Android中的Px,DPI,DIP,Density,Sp等概念
- 行人属性“Person Attribute Recognition with a Jointly-trained Holistic CNN Model”
- hbase集群搭建
- ubuntu16.04(64位)兼容32位程序
- 表
- js 解决 select option 过多 ,筛选过滤功能
- 利用SWIG转换C++接口到Java接口
- javaSE实战--(API)中的集合框架
- 极光推送
- js 之间的页面传值的
- 根据图片地址生成ImageView对象
- hibernate生成数据表时报错:HHH000388: Unsuccessful: create table emp (empid integer generated by default as i
- Banner轮播图