注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 Pass4sure CCNA640-802 v3..
 帮助

数据库 教程2


2007-11-19 13:34:49
 标签:Oracle 数据库 速成   [推送到技术圈]

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://772708707.blog.51cto.com/259734/51203
//创建表空间,表空间用于存储数据库对象,如表、视图、存储过程等。下面的语句创建一个名为myspace的表空间,数据物理文件为D:\oracle\myspace.dbf,大小为100M
//create tablespace myspace datafile 'D:\oracle\myspace.dbf' size 100M;
//删除表空间,先执行下面的sql然后删除物理文件
//drop tablespace myspace;
//创建用户,用户名为test密码为test。该用户创建的表默认保存在myspace表空间内。多个用户可以共享一个表空间
//create user test identified by test default tablespace myspace temporary tablespace temp;
//为用户授权,一般只为用户授予connect,resource角色,因为dba角色权限太高,容易破坏数据库,resource角色权限已经很高。用户的角色权限可以传递
//grant connect,resource,dba to test;
/*
创建表和删除表
指定表列名,数据类型,长度限制,列值完整性约束
主键约束保证惟一性和非空性
*/
//create table users (
// id integer default 0, //default 约束默认值
// uname varchar2(10) not null unique,  //not null 约束列值不允许为空,unique不允许值重复
// birthday date,
// height number(3,2),
// folk varchar2(4),
// primary key (id)
//);
//drop table users;  //注意删除表,所有的数据都会丢失
//修改表
//alter table users add (weight number(4,1) ,email varchar2(20));  //增加表列
//alter table users drop column weight;          //删除表列
//alter table users modify (email varchar2(100));       //改变列大小会受列已有数据的限制,如已有数据的类型,长度等
//删除表主键
//alter table users drop primary key;
//增加表主键
//alter table users add primary key (id);
/*
视图
视图的作用:
 视图能够简化用户的操作。
 视图机制可以使用户以不同的方式看待同一数据
 视图对数据库的重构提供了一定程度的逻辑独立性
 视图可以对机密的数据提供安全保护
创建视图读法:
CREATE VIEW <视图名> [(列名1,列名2,......,列名n)] AS <子查询> [WITH CHECK OPTION];
*/
//创建视图
//create view v_users as select * from users;
//创建有条件选择的视图,用户只能看到身高大于1.75的用户姓名和身高信息
//create view v_users as select uname,height from users where height>1.75;
//drop view v_users;
/*
基本查询语法
SELECT <目标列组>
     FROM <数据源>
     [WHERE <元组选择条件> ]
     [GROUP BY <分列组> [HAVING  <组选择条件> ]]
     [ORDER BY <排序列1>  <排序要求1>  [,…n]];
*/
//查询所有记录的所有字段
//select * from users;
//查询部分字段
//select id,name,folk from users;
//插入数据,注意主键和非空字段必须给定输入值
//不指定列名,输入的值顺序必须和数据库字段顺序(创建表时的顺序)一致;指定的值要和数据库字段同样多
//insert into users values (1,'user1',sysdate,1.75,'汉','user1@email.com'); commit ;
//按指定列的顺序插入数据,并且可以只插入部分数据
//insert into users(id,height,uname,folk) values (2,1.78,'user2','汉'); commit ;
//插入测试数据
//insert into users ( ID, UNAME, BIRTHDAY, HEIGHT, FOLK, EMAIL) values (3,'USER3',sysdate,1.74,'鲜','user3@email.com');
//insert into users ( ID, UNAME, BIRTHDAY, HEIGHT, FOLK, EMAIL) values (4,'user4',current_timestamp,1.76,'鲜','user4@email.com');
//insert into users ( ID, UNAME, BIRTHDAY, HEIGHT, FOLK, EMAIL) values (5,'USER5',current_date,1.85,'汉','user5@email.com');
//commit ;
//
//rollback ;
//insert into users (id,uname,height,folk) values (6,'user6',1.73,'鲜');
//insert into users  values (7,'user7',    to_date('1980-01-01','yyyy-mm-dd')    ,1.78,'汉','user7@email.com');
//insert into users(id,uname,birthday,height)
// select id+1 , substr(uname,1,length(uname)-1)||(id+1), sysdate,1.99 from users
//   where id = (select max(id) from users);
//select * from users where id > 5;
//rollback ;

//修改数据,注意一定要用where子句限定条件,否则会影响表中所有的记录(如:update users set height = 1.75 所有人的身高都会变为1.75)
//update users set height = 1.75,birthday=to_date('1982-08-15','yyyy-mm-dd') where id =1 ; commit ;
//删除数据,与update一样要有严格的where子句限制
//delete from users where id = 4; commit ;
/*
DELETE 不能删除个别的字段,它对于给定表只能删除整条记录
与INSERT 和UPDATE 一样,删除一个表中的记录可能会导致与其它表的引用完整性问题。当对数据库进行修改时一定在头脑中有这个概念。
DELETE 语句只会删除记录不会删除表,如果要删除表需使用DROP TABLE命令
*/
//Oracle不能创建自增列,当需要实现基于数据库的自增列需要使用sequence对象,创建sequence简单语句如下:
//create sequence seq_pkgen;
//查看sequence的当前值
//select seq_pkgen.currval from dual;
//使用sequence对象实现列自增
//insert into users(id,uname,folk) values(seq_pkgen.nextVal,user3,'鲜'); commit ;
/*
select 语句完整语法:
[select …] [from …] [where …] [group by …] [having …] [order by…]
除了变量值外,查询语句对大小写并不敏感。
所以 SeLeCT 与 sELEct 以及 SELECT 是相同的。
*/
//查询所有数据
//select * from users;
//条件查询
//select * from users where id = 5;
//选取部分字段
//select id,uname from users;
//select users.uname from users;
//可以为列和表使用别名,用as关键字
//select uname as username from users as myusers;  //列别名和表别名是可以单独使用的
//或
//select uname username from users;
//select myusers.uname username from users myusers;
//列别名是username,表别名是myusers,可以用myusers.uname取得uname字段
/*
SQL中的运算符
数学运算符+, -, *, /
字符串连接符...||... or concat(...,...)
*/
//数字和字符常量的计算
//select 1+2,5-6,2*3,6/5,'abc'||'def',concat('abc','def') from dual;
//对字段进行运算
//select id+height,id*height from users;
//常量与字段混合使用
//select uname||' email : '||email from users;
/*
逻辑运算符and, or, not ,[not ]in, any ,all,exists,[not ]between .. and ,is [not] null,[not] like
*/
//select * from users where uname='a' and height = 20;
//select * from users where uname='a' or height = 20;
//select * from users where uname='a' and height = 20;
//select * from users where uname='a' and height = 20 or uname='b';
//select * from users where exists (select * from users where id >= 5);
//select * from users where exists
// (select * from user_contact where users.id = 1 and user_contact.address = '北京市海淀区');
//同上
//select * from users ,user_contact where  users.id = 1 and user_contact.address = '北京市海淀区';
//select * from users where id in (1,2);
//select * from users where id > any (1,2);
//select * from users where id > all (1,2);
//select * from users where email is null;
//select * from users where email is not null;
//查询用户名为user1和user2的用户
//select * from users where uname in ('user1','user2') ;
//查询除了user1和user3外的所有用户
//select * from users where uname not in ('user1','user3') ;
//查询身高在1.72和1.75之间的记录,包括1.72和1.75
//select * from users where height between 1.72 and 1.77;
//等价的语句
//select * from users where height >= 1.72 and height <= 1.77;
//查询email是null的记录,注意null与''是不同的,null代表未知的意义,''是空字符串
//select * from users where email is null;
//select * from users where height is not null;
//select * from users where email like 'u%';
//select * from users where uname like 'user_';
//通过lower把字段内容变为小写再查询,这样实现不分大小写的查询
//select * from users where lower(email) like 'user%';
//变为大写
//select upper(uname),folk,email from users where upper(email) like 'USER%';
//可以用()对查询条件分组,改变优先级
//select * from users where (uname='user1' and email='user1@email.com') or email='user3@email.com';
/*
比较运算符=, >=, <=, <>, !=,^=
*/
//select * from users where height > 1.76;
//select * from users where height >= 1.76;
//select * from users where height <> 1.76;
//select * from users where height != 1.76;
//select * from users where height ^= 1.76;
/*
组合查询操作符和其他SQL操作符
语法:
 <查询1> <组合操作符> <查询2>
union 并集
minus 差查询
intersect 交集
all
distinct 抑制重复值
*/
//select * from users where id >3 union select * from users where id <5 ;
//select * from users where id >1 minus select * from users where id <3 ;
//select * from users where id >1 intersect select * from users where id <3 ;
//select avg(all height) from users; //一般与统计函数一起使用
//select distinct folk from users;
 
本文转自程式先锋网站 www.javabiz.cn

本文出自 “程式先锋Java培训” 博客,请务必保留此出处http://772708707.blog.51cto.com/259734/51203





    文章评论
 
2007-11-19 22:27:40
已经收藏,多谢

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: