首页 > 数据库 > Oracle > 从零开始学Oracle—基本语法解析
create alter drop truncate 开头的语句 truncate table <表名>
特点:<1>建立和修改数据对象
<2>建立和修改直接存入库中,直接生效
创建表空间:Create tablespace Product datafile 'D:testaa.DBF' size 100M
删除表空间:Drop tablespace Product
创建用户: Create user zhangsan identified by "welcome" default tablespace Product
修改用户: Alter user zhangsan identified by "Hello"
授予用户连接的权限: grant connect to zhangsan;
撤销用户连接权限: revoke connect from zhangsan;
授予用户在表空间创建对象的权限
grant unlimited tablespace to
zhangsan;
grant resource to zhangsan;
系统特权:unlimited tablespace表示用户可以在任意表空间中创建对象,且此权限只能授予用户而不能授予角色
resource角色:给用户RESOUCE权限后,用户就拥有在所有表空间上建表权限
授予用户zhangsan对Emp表增、删、查、改的权限
grant select on scott.empa to
y2;
delete
update
insert
all (=select,delete,update,insert)
with grant option 授予用户权限,则接受权限的用户可以将此权限授予其他用户.
(1).建表,并指定表空间
示例: Create table tab1
(
no number(4),
name varchar2(10)
)tablespace Product;
学生表 Student
Create table student
(
xh number(4) primary key, --学号
name varchar2(10) not null, --姓名
sex char(2) check (sex in ('男','女')),--性别
birthday date,--生日
sal number(7,2), --奖学金
classid number(2) references class(classid)
--班级
); 班级表class
Create table class
(
classid number(2), --班级编号
cname varchar2(20) --班级名字
);
添加字段(学生所在班级classid)
alter table student add (classid number(2));
修改字段的长度
alter table student modify (xm varchar2(12)) ;
修改字段的类型(不能有记录的)
alter table student modify (xh varchar2(5));
删除一个字段
alter table student drop column sal;
删除表
drop table student;
表的名字修改
rename student to stu;
字段如何改名字
--先删除
a)alter table student drop column sal;
--再添加
b)alter table student add (salary number(7,2));
Number数据类型的存储范围
精度 存储字节数
1 - 9 5
10-19 9
20-28 13
29-38 17
Oracle数据操作语言 DML
select
insert
delete
update
特点:<1>对数据起作用的
<2> 这些语句的修改是在内存中发生的
要想改动存入库中必须要commit语句
查看当前用户的所有权限
select * from
session_privs;
查看当前用户下的所有表
select * from tab where
tabtype='TABLE';
3. TCL(事务控制语句) Transaction Control Language
commit; 提交 修改保存到数据库中
rollback; 回滚 取消内存中的改动
savepoint;保存点 分解事务的 把事务变小
DDL语句 会自动提交以前未提交的事务
关闭SQLplus工具 也会自动提交未提交的事务的
事务 -- 就是一个完整的对数据的DML操作
所有事务 都是要明确的提交和回滚的
--转账
update 账目表
set 钱=钱-500
where 帐号='A';
update 账目表
set 钱=钱+500
where 帐号='B';
commit;
事务何时存在 DML语句中除select以外都会有事务
《《《《《《《注意》》》》》 / 重复运行上一条SQL语句
commit; 结束上一个事务 并且开始一个新的事务
update student set sal = null where xh =1000;
savepoint c111;
insert into student(xh,name,sex) values (1004,'MIKE','男');
rollback to c111; --撤销了插入的数据
rollback; --从c111这个点回滚到事务的开始点
《SQLPLUS规则》
a)DML语句后跟上DDL语句 DML语句的事务会被自动提交
b)exit/quit命令 退出 SQLPLUS环境时也会自动提交事务
点小叉子关闭sqlplus窗口 事务都自动回滚了
c)非法操作是不能提交事务的 ,只能导致事务回滚
revoke 撤销权限
权限 select ,insert,delete,update
all (select ,insert,delete,update 总和)
角色 connect (登陆数据库),resource(建立表和对象)
如何建一个自己的用户?
必须是超级用户才能建用户
--连接到超级用户
connect system/manager
--建立用户名zhangsan 密码m123
create user zhangsan identified by m123;
--授予必要的权限connect 你能够连接
resource 你能建表不受空间的限制,建立对象
grant connect,resource to zhangsan;
--这个普通用户就建好了 和scott用户的权限是一样的
grant DBA to zhangsan; --张三的权限和System一样
--改张三的密码
<1> 自己改自己的密码
connect
zhangsan/m123
密码改为了mm1
alter user zhangsan
identified by mm1;
<2> 超级用户来改
connect system/manager
alter user zhangsan identified by mm1;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22057866/viewspace-1116439/ ,如需转载,请注明出处,否则将追究法律责任。