1. --建表
  2. -- 关键字 create
  3. -- 用法 :
  4. /*
  5. create table table_name

  6. lie1_name 数据类型 是否为空,
  7. lie2_name 数据类型 是否为空,
  8. lie3_name 数据类型 是否为空,
  9. ……………………………………
  10. );
  11.  
  12. */
  13. create table infos
  14. (
  15. stuid varchar2(7) not null ,
  16.  
  17. stuname varchar2(10) not null ,
  18.  
  19. gender varchar2(2) not null ,
  20.  
  21. age number(2) not null ,
  22.  
  23. seat number(2) not null ,
  24.  
  25. enrolldate date ,
  26.  
  27. studress varchar2(50) ,
  28.  
  29. classno varchar2(4) not null
  30. ) ;
  31.  
  32. -- 添加约束
  33.  
  34. --添加主键
  35. alter table infos add constraint pk_infos primary key(stuid) ;
  36.  
  37. --check 约束
  38. -- 性别只能是男或女
  39. --作用: 条件限制
  40. alter table infos add constraint ck_infos_gendertest check(gender='男' or gender='女') ;
  41.  
  42. --唯一约束
  43. --作用:使该列内容不重复
  44. alter table infos add constraint un_stuname unique(stuname) ;
  45.  
  46. --日期格式转换
  47. select to_char(sbirthday , 'yyyy-mm-dd hh24:mi:ss') from student ;
  48.  
  49. --查询系统时间
  50. select sysdate from dual ;
  51.  
  52. -- dual 伪列 ,只有一行一列数据
  53.  
  54. --绝对值函数
  55. --abs() 括号内是数值型或可以隐形转换成数值型
  56. select abs(122),abs(-123),abs(0) from dual ;
  57.  
  58. --数据添加
  59.  
  60. -- insert into 表名 valus
  61. insert into student(sno,sname,ssex) values ('','小明','男') ;
  62.  
  63. --添加日期
  64. --需要类型转换 to_date
  65. insert into student values(to_date('1990/2/3','yyyy/mm/dd')) ;
  66.  
  67. --提交事务
  68.  
  69. commit ;
  70.  
  71. --回滚事务
  72.  
  73. rollback ;
  74.  
  75. --数据的复制
  76.  
  77. -- insert into 新的表名 select * from 要复制的表名 ;
  78.  
  79. --查询数据去除重复
  80. --distinct
  81. --select distinct 列名 from 表名 ;
  82. -- * 代表所有字段信息
  83. -- != <> 表示不等于
  84. --|| 表示拼接
  85.  
  86. --修改表
  87. --update 表名 set 条件或添加内容
  88. update student set sclass = '' where sname='小明' ;
  89.  
  90. update student set sclass = '' where sclass is null ;
  91.  
  92. --删除表
  93. --delete
  94. delete student where sname = '小明' ;
  95.  
  96. --快速清空一个表
  97. --truncate
  98. --truncate table 表名 ;
  99.  
  100. --排序
  101.  
  102. --order by asc/desc 正序/倒序
  103. --放于where条件后
  104.  
  105. --分组
  106. --group by 列名
  107.  
  108. --查询位于两个数值之间的记录
  109. --两种方法
  110.  
  111. select degree from score where degree >70 and degree < 85 ;
  112. --between 后面必须是小的数值
  113. select degree from score where degree between 70 and 85 ;
  114.  
  115. --查询 位于66,77,88 中的成绩
  116.  
  117. select degree from score where degree=66 or degree=77 or degree=88 ;
  118.  
  119. select degree from score where degree in (66,77,88) ;
  120.  
  121. /*
  122. 排序用法 order by
  123.  
  124. asc 正序,默认 ,可不加
  125.  
  126. desc 降序 ;
  127.  
  128. 语法: order by 字段名1 asc/desc ,字段名2 asc/desc ...
  129.  
  130. 字段在前的优先级高, 先按字段1排序,再按字段2排序
  131.  
  132. 有条件限制语句的话,放在where语句后面
  133.  
  134. */
  135.  
  136. -- 去除查询数据中的重复记录
  137. --distinct
  138. --用法
  139. --select distinct lie_name from biao_name ;
  140. --实例
  141. select distinct depart from teacher ;
  142.  
  143. -- 查询两个数据之间
  144.  
  145. -- 比较运算 例:
  146. select * from score where degree > 60 and degree < 80 ;
  147.  
  148. -- 关键字:between 小值 and 大值 例:
  149. select * from score where degree between 60 and 80 ;
  150.  
  151. -- 内置函数
  152.  
  153. -- 聚合函数 返回单个值
  154. --- 记录条数(列名内容不为空,一般统计主键列)
  155.  
  156. select count(*) from student where sclass = '';
  157.  
  158. -- 成绩求和
  159.  
  160. select sum(degree) 总成绩 from score ;
  161.  
  162. -- 平均成绩
  163.  
  164. select avg(degree) 平均值 from score ;
  165.  
  166. -- 最高分
  167.  
  168. select max(degree) 最高分 from score ;
  169.  
  170. -- 最低分
  171.  
  172. select min(degree) 最低分 from score ;
  173.  
  174. -- 伪列
  175. -- rownum 内置列
  176. select * from score where rownum = 1 ;
  177.  
  178. --查询每门课的平均成绩大于80
  179. -- cno 分组,在 内置函数
  180. select cno , avg(degree),count(cno) from score group by cno having avg(degree) > 80 ;
  181.  
  182. select * from (select cno , avg(degree) a,count(cno) from score group by cno ) where a> 80 ;
  183.  
  184. --纵向查询
  185. --两个表之间查询的列数要相同,对应列数的数据类型要相同
  186.  
  187. select sname,ssex,sbirthday from student
  188. union
  189. select tname,tsex,tbirthday from teacher ;
  190.  
  191. --模糊查询
  192.  
  193. --查询姓王的学生信息
  194. select * from student where sname like '王%' ;
  195. --% 代表任意个数的字符
  196. -- 一个下划线代表一个字符 _
  197.  
  198. -- 内置函数
  199.  
  200. --下限值
  201.  
  202. select floor(123.12) from dual ;
  203.  
  204. --上限值
  205.  
  206. select ceil(123.12) from dual ;
  207.  
  208. --四舍五入
  209.  
  210. select round(123.89) from dual ;
  211.  
  212. --保留位数四舍五入
  213.  
  214. select round125.456,2from dual ;
  215.  
  216. --直接舍掉
  217. --作用: 格式化数据,统一格式
  218.  
  219. select trunc(123.456) from dual ;
  220. --并不会四舍五入
  221. select trunc(123.456,2) from dual ;
  222.  
  223. --绝对值
  224.  
  225. select ABS(-23) from dual ;
  226.  
  227. --求余数 ,求模
  228.  
  229. select mod(78,69) from dual ;
  230.  
  231. --计算字符串长度
  232.  
  233. select tname,length(tname) from teacher ;
  234.  
  235. --去空格
  236. --去前后空格
  237. select trim(' a b cf ') from dual ;
  238. --去前空格 ltrim
  239. select ltrim(' sdf ') from dual ;
  240. --去后空格 rtrim
  241. select rtrim(' sdf ') from dual ;
  242.  
  243. --查找替换
  244.  
  245. select replace(' s df g ',' ') from dual ;
  246.  
  247. select sname,replace(sname,'王','李') from student ;
  248.  
  249. --查找字符串(空格也算一个),从1开始,找不到返回0
  250.  
  251. select instr('sd wef','w') from dual ;
  252.  
  253. --截取字符串
  254. --3 第几位开始截取
  255. --5 截取长度
  256. select substr('sdfga fgh',3,5) from dual ;
  257.  
  258. select sname,substr(sname,1,1) || '同学' from student ;
  259.  
  260. --null值处理
  261.  
  262. select nvl(degree,0) from score ;
  263.  
  264. select nvl2(degree,55,0) from score ;
  265.  
  266. select t.*,decode(ssex,'男','','女','') from student t ;
  267.  
  268. --返回当前用户登录名
  269.  
  270. select user from dual ;
  271.  
  272. ---取得序列的当前值
  273. select sq_test.nextval from dual ;
  274.  
  275. select sq_test.currval from dual ;
  276.  
  277. insert into weadafa values(sq_test.nextval,'王五') ;
  278. commit ;
  279. select * from weadafa ;
  280.  
  281. --视图
  282.  
  283. create or replace view v_score as
  284. select t.*,s.sname,c.cname
  285. from score t,student s,course c where t.sno=s.sno and t.cno=c.cno ;

Oracle ——————建表、select、视图的更多相关文章

  1. 5.oracle建表的时候同时创建主键,外键,注释,约束,索引

    5.oracle建表的时候同时创建主键,外键,注释,约束,索引 1 --主键 )); ) ,constraint aba_pr primary key(id,name1)); --外键 )); --复 ...

  2. oracle 建表 主键自增序列/////

    oracle 建表 主键自增序列 (2011-10-12 11:59:22) 转载▼ 标签: 杂谈 分类: oracle SQL> create table sms_activity(  2   ...

  3. Oracle建表提示SQL 错误: ORA-00904: : 标识符无效

    Oracle建表提示: 错误报告:SQL 错误: ORA-00904: : 标识符无效00904. 00000 -  "%s: invalid identifier"*Cause: ...

  4. PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写

    原文:PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入T ...

  5. oracle 建表时显示ORA-00984: 列在此处不允许

      oracle 建表时显示ORA-00984: 列在此处不允许 CreationTime--2018年7月19日16点10分 Author:Marydon 1.情景展示 使用plsql建表时,报错 ...

  6. oracle 建表时显示ORA-00904无效的标识符

      oracle 建表时显示ORA-00904无效的标识符 CreationTime--2018年7月19日16点03分 Author:Marydon 1.情景展示 使用plsql建表时,报错 字段展 ...

  7. Oracle建表

    1.oracle数据库中的多种数据结构: 1.表结构            存储数据 2.视图 一张表或多张表中数据的字节 3.sequence 主要用来生成主键值 4.index 提高检索性能 我们 ...

  8. SQL SERVER 生成ORACLE建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...

  9. Oracle建表插数据等等

    Oracle的表的管理: 表名和列的命名规则,详见 数据库命名规范 . 必须以字母开头 . 长度不能超过30个字符 . 不能使用Oracle的保留字 . 只能使用如下字符 column_name-Z, ...

随机推荐

  1. android 学习随笔十六(广播 )

    1.广播接收者 BroadcastReceiver 接收系统发出的广播 现实中的广播:电台为了传达一些消息,而发送的广播,通过广播携带要传达的消息,群众只要买一个收音机,就可以收到广播了  Andro ...

  2. ubunu下用命令设置壁纸

    ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...

  3. JVM学习笔记(二)------Java代码编译和执行的整个过程【转】

    转自:http://blog.csdn.net/cutesource/article/details/5904542 版权声明:本文为博主原创文章,未经博主允许不得转载. Java代码编译是由Java ...

  4. OC基础数据类型-NSData

    1.NSData,数据,当我们需要把一些信息写入到文件里或发送到网络上,我们需要把这些数据转换下,变成纯粹的0.1字符流 1 NSString * str = @"hello, world! ...

  5. PHP stat() 函数 返回关于文件的信息。

    定义和用法 stat() 函数返回关于文件的信息. 语法 fstat(file) 参数 描述 file 必需.规定要检查的文件. 说明 获取由 file 指定的文件的统计信息.如果 file 是符号连 ...

  6. android使用其他应用打开文件

    根据文件的MIME类型来判断,手机中有哪些应用可以打开这个文件,然后把应用在弹窗列表中显示 /** * 打开文件 * * @param file */ public static void openF ...

  7. ACM题目————棋盘问题

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  8. 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏

    Clique in the Divisibility Graph time limit per test1 second memory limit per test256 megabytes inpu ...

  9. Animation Play/Stop测试

    测试结果: 1.当切换不同动画播放,可以直接调用Play或者CrossFade.会立即切过去 2.当相同动画再次播放,又想打断重头再播,需要先调用Stop. anim.Play("Test1 ...

  10. Mybatis 和 Spring配置

    一.使用的jar包就不详细讲解了,下载了Mybatis 和 Spring 的jar包基本上都添加上去了. 一图概括:(这是我使用的ar包,有些不是Mybatis 和 Spring 的 ) 二. web ...