oracle 10g基础

来源:互联网 发布:unity3d ugui教程 编辑:程序博客网 时间:2024/04/29 06:48
  1. 操作表
  2.  1创建新表
  3. 1.1从查询到的表创建表
  4.  create table temp as select stuName,stuNo,stuSex from stuInfo where stuAge>25;
  5. 1.2创建新表
  6. /*学生信息表*/
  7. create table stuInfo(
  8.     stuName varchar2(10) ,
  9.     stuNo varchar2(10),
  10.     stuSex varchar2(4),
  11.     stuAge number(2),
  12.     stuSeat number(10),
  13.     stuAddress varchar2(400));
  14. /*学生成绩表*/
  15. create table stuMark(
  16.     examNo varchar2(10),
  17.     stuNo varchar2(10),
  18.     writtenExam number(4),
  19.     labExam number(4));
  20. 2 修改表
  21. 2.1 增加字段
  22. alter table stuInfo add(Zip number(6));
  23. 2.2 删除字段
  24. alter table stuInfo drop column Zip
  25. 2.3 修改字段类型
  26. alter table stuInfo modify(Zip varchar2(6));
  27. 2.4修改字段大小
  28. alter table stuInfo modify(Zip number(4));
  29. 2.5 删除表
  30.     drop table stuInfo
  31. 3约束
  32. 3.1添加约束
  33. alter table stuInfo add constraint PK_stuNo primary key(stuNo);
  34. alter table stuInfo add constraint CK_stuSex check(stuSex in('男','女'));
  35. alter table stuInfo add constraint CK_stuAge check(stuAge between 15 and 40);
  36. alter table stuInfo add constraint CK_stuSeat check(stuSeat between 1 and 30);
  37.     
  38. alter table stuMark add constraint PK_ExamNo_stuMark  primary key(examNo);
  39. alter table stuMark add constraint FK_stuNo_stuMark  foreign key(stuNo) references stuInfo(stuNo);
  40.  select stuName,decode(stuSex,'男','男同志'),
  41.     (stuSex,'女','女同志')
  42.      from stuInfo;
  43. alter table stuInfo modify(stuSex not null);
  44. 3.2删除约束
  45. 3.2.1删除普通约束
  46. alter table stuInfo drop constraint CK_stuSex;
  47. 3.2.2删除被外键参照的主键约束
  48. alter table stuInfo drop primary key PK_StuNo
  49. 4索引
  50. 4.1创建索引
  51. create index stuName_index on stuInfo(stuName);
  52. 4.2删除索引
  53. drop index stuName_index;
  54. 5创建序列
  55. 5.1 创建序列
  56. create sequence stuSeat_identity
  57. minvalue 1
  58. maxvalue 99999999
  59. start with 1
  60. increment by 1
  61. cache 2
  62. 5.2触发器实现字段列自增长
  63. create table stu(
  64.  stuID number(10),
  65.  stuName varchar2(20));
  66. create sequence autoId start with 1 increment by 1 cache 200;
  67. create or replace trigger getautoId
  68.   before insert on stu  
  69.   for each row
  70. declare
  71.   -- local variables here
  72. begin
  73.   select autoId.Nextval into :new.stuId from dual;
  74. end getautoId;
  75. insert into stu values(null,'s');
  76. insert into stu values(null,'s1');
  77. insert into stu values(null,'s2');
  78. insert into stu values(null,'s3');
  79. 6视图
  80. 6.1 创建视图
  81. create or replace view v_stuInfo as  select * from stuInfo;
  82. 6.2 删除视图
  83. drop view v_stuInfo;
  84. 7同义词
  85. 7.1 创建同义词
  86. create synonym st for System.stuInfo;
  87. 7.2 删除同义词
  88. drop synonym st;
  89. 8 数据操作语句
  90. 8.1 插入数据
  91. insert into stuInfo values('MARK','s25301','男',18,stuseat_identity.nextval,'北京海淀');
  92. insert into stuInfo values('andy','s25303','女',22,stuseat_identity.nextval,'河南洛阳');
  93. insert into stuInfo values('JACK','s25302','男',31,stuseat_identity.nextval,'武汉武昌');
  94. insert into stuInfo values('zerolin','s25304','男',28,stuseat_identity.nextval,'新疆威武哈'); 
  95. insert into stuMark values('s271811','s25303',90,56);
  96. insert into stuMark values('s271813','s25302',58,90);
  97. insert into stuMark values('s271816','s25301',87,82);
  98. insert into stuMark values('s271819','s25304',66,48);
  99. 8.2 更新语句
  100. update stuInfo set stuName='Oracle' where stuAge>27;
  101. 8.3 数据合拼语句
  102. merge into depat_13_temp a
  103. using department_13 b
  104. on(a.department_id = b.department_id)
  105. when matched then
  106.   update set
  107.      a.department_name = b.department_name,
  108.      a.manager_id = b.manager_id,
  109.      a.location_id = b.location_id
  110. when not matched then
  111.   insert(a.department_id, a.department_name, a.manager_id, a.location_id)
  112.   values(b.department_id, b.department_name, b.manager_id, b.location_id);
  113. 9 查询语句
  114. 9.1 带算术表达式的select语句
  115. select ExamNo,stuNo,writtenExam,labExam+100 from stuMark;
  116. 9.2 带连接表达式的select语句
  117.  select ExamNo||'的学号是:'||stuNo||'笔记成绩是:'||writtenExam||'机试成绩是:'||labExam from stuMark;
  118. 9.3 字段别名
  119.  select ExamNo "考号",stuNo "学号",writtenExam "笔记成绩是",labExam "机试成绩是" from stuMark;
  120. 9.4 比较操作符
  121. between..and 
  122. in
  123. like
  124. is null
  125. 9.5 比较操作的逻辑运算符
  126. and  or
  127. select * from stuInfo where stuSex='男' or stuSex='女';
  128. select * from stuInfo where stuSex='男' and stuAge>27;
原创粉丝点击