创建表空间
- create tablespace qnhouse
- --表空间文件路径
- datafile 'E:\qnhost\qnhouse.dbf'
- --表空间文件大小
- size 100M;
创建用户
- create user qnhouse
- --登录密码
- identified by qnhouse
- --默认的表空间
- default tablespace qnhouse;
为用户授权
- --RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构。
- --CONNECT:拥有Connect权限的用户只可以登录Oracle,不可以创建实体,不可以创建数据库结构。
- grant connect,resource to qnhouse;
建表与约束
- --区域表
- create table DISTRICT
- (
- id NUMBER not null,
- name VARCHAR2() not null,
- --约束
- --主键约束
- constraint PK_district_id primary key(id)
- );
- --街道表
- create table STREET
- (
- id NUMBER not null,
- name VARCHAR2(),
- district_id NUMBER, --区域id
- --约束
- constraint PK_STREET_id primary key(id),
- --外键约束
- constraint FK_STREET_district_id foreign key (district_id) references district(id)
- );
- --户型表
- create table housetype
- (
- id NUMBER,
- name VARCHAR2(),
- --约束
- constraint PK_housetype_id primary key(id)
- );
- --用户表
- create table USERS
- (
- id NUMBER not null,
- name VARCHAR2(),
- password VARCHAR2(),
- telephone VARCHAR2(),
- username VARCHAR2(),
- --默认值
- isadmin VARCHAR2() default not null,
- --约束
- constraint PK_USERS_id primary key(id),
- --唯一约束
- constraint UQ_users_name unique(name),
- --检查约束,密码不能少于6位
- constraint CK_users_password check (length(password)>=)
- );
- --房屋信息表
- create table HOUSE
- (
- id NUMBER,
- user_id NUMBER, --用户ID
- type_id NUMBER, --户型ID
- title NVARCHAR2(),
- description NVARCHAR2(),
- price NUMBER,
- pubdate DATE,
- floorage NUMBER,
- contact VARCHAR2(),
- street_id NUMBER, --街道ID
- --约束
- constraint PK_HOUSE_id primary key(id),
- constraint FK_HOUSE_user_id foreign key (user_id) references users(id),
- constraint FK_HOUSE_type_id foreign key (type_id) references housetype (id),
- constraint FK_HOUSE_street_id foreign key (street_id) references STREET(id)
- );
序列
- --序列
- create sequence seq_qnhouse
- --递增值
- increment by
- --开始值
- START WITH ;
自增触发器
- --用户主键自增
- create or replace trigger tri_users_id
- before insert on users
- for each row
- begin
- --用序列的值填到主键
- select seq_qnhost.nextval into :new.id from dual;
- end;
- /
以上就是简单的oracle sql 语句的详细内容,更多关于简单的oracle sql 语句的资料请关注九品源码其它相关文章!