Oracle ——————建表、select、视图
- --建表
- -- 关键字 : create
- -- 用法 :
- /*
- create table table_name
- (
- lie1_name 数据类型 是否为空,
- lie2_name 数据类型 是否为空,
- lie3_name 数据类型 是否为空,
- ……………………………………
- );
- */
- create table infos
- (
- stuid varchar2(7) not null ,
- stuname varchar2(10) not null ,
- gender varchar2(2) not null ,
- age number(2) not null ,
- seat number(2) not null ,
- enrolldate date ,
- studress varchar2(50) ,
- classno varchar2(4) not null
- ) ;
- -- 添加约束
- --添加主键
- alter table infos add constraint pk_infos primary key(stuid) ;
- --check 约束
- -- 性别只能是男或女
- --作用: 条件限制
- alter table infos add constraint ck_infos_gendertest check(gender='男' or gender='女') ;
- --唯一约束
- --作用:使该列内容不重复
- alter table infos add constraint un_stuname unique(stuname) ;
- --日期格式转换
- select to_char(sbirthday , 'yyyy-mm-dd hh24:mi:ss') from student ;
- --查询系统时间
- select sysdate from dual ;
- -- dual 伪列 ,只有一行一列数据
- --绝对值函数
- --abs() 括号内是数值型或可以隐形转换成数值型
- select abs(122),abs(-123),abs(0) from dual ;
- --数据添加
- -- insert into 表名 valus
- insert into student(sno,sname,ssex) values ('','小明','男') ;
- --添加日期
- --需要类型转换 to_date
- insert into student values(to_date('1990/2/3','yyyy/mm/dd')) ;
- --提交事务
- commit ;
- --回滚事务
- rollback ;
- --数据的复制
- -- insert into 新的表名 select * from 要复制的表名 ;
- --查询数据去除重复
- --distinct
- --select distinct 列名 from 表名 ;
- -- * 代表所有字段信息
- -- != 或 <> 表示不等于
- --|| 表示拼接
- --修改表
- --update 表名 set 条件或添加内容
- update student set sclass = '' where sname='小明' ;
- update student set sclass = '' where sclass is null ;
- --删除表
- --delete
- delete student where sname = '小明' ;
- --快速清空一个表
- --truncate
- --truncate table 表名 ;
- --排序
- --order by asc/desc 正序/倒序
- --放于where条件后
- --分组
- --group by 列名
- --查询位于两个数值之间的记录
- --两种方法
- select degree from score where degree >70 and degree < 85 ;
- --between 后面必须是小的数值
- select degree from score where degree between 70 and 85 ;
- --查询 位于66,77,88 中的成绩
- select degree from score where degree=66 or degree=77 or degree=88 ;
- select degree from score where degree in (66,77,88) ;
- /*
- 排序用法 order by
- asc 正序,默认 ,可不加
- desc 降序 ;
- 语法: order by 字段名1 asc/desc ,字段名2 asc/desc ...
- 字段在前的优先级高, 先按字段1排序,再按字段2排序
- 有条件限制语句的话,放在where语句后面
- */
- -- 去除查询数据中的重复记录
- --distinct
- --用法
- --select distinct lie_name from biao_name ;
- --实例
- select distinct depart from teacher ;
- -- 查询两个数据之间
- -- 比较运算 例:
- select * from score where degree > 60 and degree < 80 ;
- -- 关键字:between 小值 and 大值 。 例:
- select * from score where degree between 60 and 80 ;
- -- 内置函数
- -- 聚合函数 返回单个值
- --- 记录条数(列名内容不为空,一般统计主键列)
- select count(*) from student where sclass = '';
- -- 成绩求和
- select sum(degree) 总成绩 from score ;
- -- 平均成绩
- select avg(degree) 平均值 from score ;
- -- 最高分
- select max(degree) 最高分 from score ;
- -- 最低分
- select min(degree) 最低分 from score ;
- -- 伪列
- -- rownum 内置列
- select * from score where rownum = 1 ;
- --查询每门课的平均成绩大于80的
- -- 先cno 分组,在 内置函数
- select cno , avg(degree),count(cno) from score group by cno having avg(degree) > 80 ;
- select * from (select cno , avg(degree) a,count(cno) from score group by cno ) where a> 80 ;
- --纵向查询
- --两个表之间查询的列数要相同,对应列数的数据类型要相同
- select sname,ssex,sbirthday from student
- union
- select tname,tsex,tbirthday from teacher ;
- --模糊查询
- --查询姓王的学生信息
- select * from student where sname like '王%' ;
- --% 代表任意个数的字符
- -- 一个下划线代表一个字符 _
- -- 内置函数
- --下限值
- select floor(123.12) from dual ;
- --上限值
- select ceil(123.12) from dual ;
- --四舍五入
- select round(123.89) from dual ;
- --保留位数四舍五入
- select round(125.456,2)from dual ;
- --直接舍掉
- --作用: 格式化数据,统一格式
- select trunc(123.456) from dual ;
- --并不会四舍五入
- select trunc(123.456,2) from dual ;
- --绝对值
- select ABS(-23) from dual ;
- --求余数 ,求模
- select mod(78,69) from dual ;
- --计算字符串长度
- select tname,length(tname) from teacher ;
- --去空格
- --去前后空格
- select trim(' a b cf ') from dual ;
- --去前空格 ltrim
- select ltrim(' sdf ') from dual ;
- --去后空格 rtrim
- select rtrim(' sdf ') from dual ;
- --查找替换
- select replace(' s df g ',' ') from dual ;
- select sname,replace(sname,'王','李') from student ;
- --查找字符串(空格也算一个),从1开始,找不到返回0
- select instr('sd wef','w') from dual ;
- --截取字符串
- --3 第几位开始截取
- --5 截取长度
- select substr('sdfga fgh',3,5) from dual ;
- select sname,substr(sname,1,1) || '同学' from student ;
- --null值处理
- select nvl(degree,0) from score ;
- select nvl2(degree,55,0) from score ;
- select t.*,decode(ssex,'男','','女','') from student t ;
- --返回当前用户登录名
- select user from dual ;
- ---取得序列的当前值
- select sq_test.nextval from dual ;
- select sq_test.currval from dual ;
- insert into weadafa values(sq_test.nextval,'王五') ;
- commit ;
- select * from weadafa ;
- --视图
- create or replace view v_score as
- select t.*,s.sname,c.cname
- from score t,student s,course c where t.sno=s.sno and t.cno=c.cno ;
Oracle ——————建表、select、视图的更多相关文章
- 5.oracle建表的时候同时创建主键,外键,注释,约束,索引
5.oracle建表的时候同时创建主键,外键,注释,约束,索引 1 --主键 )); ) ,constraint aba_pr primary key(id,name1)); --外键 )); --复 ...
- oracle 建表 主键自增序列/////
oracle 建表 主键自增序列 (2011-10-12 11:59:22) 转载▼ 标签: 杂谈 分类: oracle SQL> create table sms_activity( 2 ...
- Oracle建表提示SQL 错误: ORA-00904: : 标识符无效
Oracle建表提示: 错误报告:SQL 错误: ORA-00904: : 标识符无效00904. 00000 - "%s: invalid identifier"*Cause: ...
- PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写
原文:PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入T ...
- oracle 建表时显示ORA-00984: 列在此处不允许
oracle 建表时显示ORA-00984: 列在此处不允许 CreationTime--2018年7月19日16点10分 Author:Marydon 1.情景展示 使用plsql建表时,报错 ...
- oracle 建表时显示ORA-00904无效的标识符
oracle 建表时显示ORA-00904无效的标识符 CreationTime--2018年7月19日16点03分 Author:Marydon 1.情景展示 使用plsql建表时,报错 字段展 ...
- Oracle建表
1.oracle数据库中的多种数据结构: 1.表结构 存储数据 2.视图 一张表或多张表中数据的字节 3.sequence 主要用来生成主键值 4.index 提高检索性能 我们 ...
- SQL SERVER 生成ORACLE建表脚本
/****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...
- Oracle建表插数据等等
Oracle的表的管理: 表名和列的命名规则,详见 数据库命名规范 . 必须以字母开头 . 长度不能超过30个字符 . 不能使用Oracle的保留字 . 只能使用如下字符 column_name-Z, ...
随机推荐
- android 学习随笔十六(广播 )
1.广播接收者 BroadcastReceiver 接收系统发出的广播 现实中的广播:电台为了传达一些消息,而发送的广播,通过广播携带要传达的消息,群众只要买一个收音机,就可以收到广播了 Andro ...
- ubunu下用命令设置壁纸
ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...
- JVM学习笔记(二)------Java代码编译和执行的整个过程【转】
转自:http://blog.csdn.net/cutesource/article/details/5904542 版权声明:本文为博主原创文章,未经博主允许不得转载. Java代码编译是由Java ...
- OC基础数据类型-NSData
1.NSData,数据,当我们需要把一些信息写入到文件里或发送到网络上,我们需要把这些数据转换下,变成纯粹的0.1字符流 1 NSString * str = @"hello, world! ...
- PHP stat() 函数 返回关于文件的信息。
定义和用法 stat() 函数返回关于文件的信息. 语法 fstat(file) 参数 描述 file 必需.规定要检查的文件. 说明 获取由 file 指定的文件的统计信息.如果 file 是符号连 ...
- android使用其他应用打开文件
根据文件的MIME类型来判断,手机中有哪些应用可以打开这个文件,然后把应用在弹窗列表中显示 /** * 打开文件 * * @param file */ public static void openF ...
- ACM题目————棋盘问题
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- 周赛-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 ...
- Animation Play/Stop测试
测试结果: 1.当切换不同动画播放,可以直接调用Play或者CrossFade.会立即切过去 2.当相同动画再次播放,又想打断重头再播,需要先调用Stop. anim.Play("Test1 ...
- Mybatis 和 Spring配置
一.使用的jar包就不详细讲解了,下载了Mybatis 和 Spring 的jar包基本上都添加上去了. 一图概括:(这是我使用的ar包,有些不是Mybatis 和 Spring 的 ) 二. web ...