简单的oracle sql 语句
2013-09-03 12:20
316 查看
创建表空间
创建用户
为用户授权
建表与约束
序列
自增触发器
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(50) not null, --约束 --主键约束 constraint PK_district_id primary key(id) ); --街道表 create table STREET ( id NUMBER not null, name VARCHAR2(50), 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(50), --约束 constraint PK_housetype_id primary key(id) ); --用户表 create table USERS ( id NUMBER not null, name VARCHAR2(50), password VARCHAR2(50), telephone VARCHAR2(15), username VARCHAR2(50), --默认值 isadmin VARCHAR2(5) default 0 not null, --约束 constraint PK_USERS_id primary key(id), --唯一约束 constraint UQ_users_name unique(name), --检查约束,密码不能少于6位 constraint CK_users_password check (length(password)>=6) ); --房屋信息表 create table HOUSE ( id NUMBER, user_id NUMBER, --用户ID type_id NUMBER, --户型ID title NVARCHAR2(50), description NVARCHAR2(2000), price NUMBER, pubdate DATE, floorage NUMBER, contact VARCHAR2(100), 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 1 --开始值 START WITH 1000;
自增触发器
--用户主键自增 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 case语句简单使用
- Oracle sql语句简单优化
- 『ORACLE』 SQL语句简单应用(二)(11g)
- oralce 基础知识 ,oralce 常用简单sql语句,oracle 新手必看之一
- oracle 一些简单的sql语句
- 简单oracle分页sql语句
- Oracle中简单查询、限定查询、数据排序SQL语句范例和详细注解
- oracle / sql 简单语句
- 【ORACLE】一个简单实用的对数据进行行列转换并统计的SQL语句
- oracle简单和复杂的SQL语句练习
- Oracle中简单查询、限定查询、数据排序SQL语句范例和详细注解
- 有时候执行的oracle,sql语句要查看下性能情况,可以用这个进行下简单计算和统计
- 【ORACLE】一个简单实用的对数据进行行列转换并统计的SQL语句
- Oracle动态SQL语句的简单执行
- oracle 简单sql语句
- oralce 基础知识 ,oralce 常用简单sql语句,oracle 新手必看之一
- 『ORACLE』 SQL语句简单应用(五)(11g)
- Oracle中备份表的简单sql命令语句
- Oracle 动态SQL语句(1)之简单书写