king

简单的oracle sql 语句

king 批处理 2023-02-25 511浏览 0

创建表空间

1 create tablespace qnhouse 
2     --表空间文件路径
3   datafile 'E:\qnhost\qnhouse.dbf'
4   --表空间文件大小
5   size 100M;

创建用户

 

1 create user qnhouse
2   --登录密码
3   identified by qnhouse
4   --默认的表空间
5   default tablespace qnhouse;

 

为用户授权

1 --RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构。
2 --CONNECT:拥有Connect权限的用户只可以登录Oracle,不可以创建实体,不可以创建数据库结构。
3 grant connect,resource to qnhouse;

建表与约束

 1 --区域表
 2 create table DISTRICT
 3 (
 4   id   NUMBER not null,
 5   name VARCHAR2(50) not null,
 6   --约束
 7   --主键约束
 8   constraint PK_district_id primary key(id)
 9 );
10 
11 --街道表
12 create table STREET
13 (
14   id          NUMBER not null,
15   name        VARCHAR2(50),
16   district_id NUMBER,               --区域id
17   --约束
18   constraint PK_STREET_id primary key(id),
19   --外键约束
20   constraint FK_STREET_district_id foreign key (district_id) references district(id)
21 );
22 
23 --户型表
24 create table housetype
25 (
26   id          NUMBER,
27   name        VARCHAR2(50),
28   --约束
29   constraint PK_housetype_id primary key(id)
30 );
31 
32 --用户表
33 create table USERS
34 (
35   id        NUMBER not null,
36   name      VARCHAR2(50),
37   password  VARCHAR2(50),
38   telephone VARCHAR2(15),
39   username  VARCHAR2(50),
40   --默认值
41   isadmin   VARCHAR2(5) default 0 not null,
42   --约束
43   constraint PK_USERS_id primary key(id),
44   --唯一约束
45   constraint UQ_users_name unique(name),
46   --检查约束,密码不能少于6位
47   constraint CK_users_password check (length(password)>=6)
48 );
49 
50 --房屋信息表
51 create table HOUSE
52 (
53   id          NUMBER,
54   user_id     NUMBER,           --用户ID
55   type_id     NUMBER,           --户型ID
56   title       NVARCHAR2(50),
57   description NVARCHAR2(2000),
58   price       NUMBER,
59   pubdate     DATE,
60   floorage    NUMBER,
61   contact     VARCHAR2(100),
62   street_id   NUMBER,           --街道ID
63   --约束
64   constraint PK_HOUSE_id primary key(id),
65   constraint FK_HOUSE_user_id foreign key (user_id) references users(id),
66   constraint FK_HOUSE_type_id foreign key (type_id) references housetype (id),
67   constraint FK_HOUSE_street_id foreign key (street_id) references STREET(id)
68 );

序列

1 --序列
2 create sequence seq_qnhouse
3   --递增值
4   increment by 1
5   --开始值
6   START WITH 1000;

自增触发器

 

1 --用户主键自增
2 create or replace trigger tri_users_id
3   before insert on users
4   for each row 
5 begin
6     --用序列的值填到主键
7   select seq_qnhost.nextval into :new.id from dual;
8 end;
9 /

 

 

 

 

继续浏览有关 数据库技术文章/教程 的文章
发表评论