简单的oracle sql 语句

数据库   发布日期:2025年06月17日   浏览次数:141

创建表空间

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

序列

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

自增触发器

 

  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. /

 

 

 

 

以上就是简单的oracle sql 语句的详细内容,更多关于简单的oracle sql 语句的资料请关注九品源码其它相关文章!