1、建表
补:desc 表名 用于查看表结构
2、修改表
增加列:
alter table <表名>
add(列名 数据类型);
修改列数据类型:
alter table 表名
modify (列名 数据类型);
删除字段:
alter table 表名
drop column 列名;
修改表名:
rename 旧名 to 新名;
修改列名:
alter table 表名 rename column 列名 to 新列名;
补:还有一种修改表名的方式,就是修改系统表col$,
select object_name,object_id from user_objects where object_name=表名;
得到表id,
切换到dba用户,select obj#, col#,name from col$ where obj#=得到的id;
得到列号,
update col$ set name=新列名 where obj#=得到的id and col#=得到的列号;
删除表:
drop table 表名;
补:表名,列名,关键字不分大小写,但内容是区分大小写的。
2、crud
insert update delete select
insert数据类型一致(可转也行),如可以把‘123’给number
insert into 表名 (字段) values(数据);
字符和日期数据放在单引号中。
放空值:指定null 不指定 字符串可以给一个'';
update 表名 set 列名=表达式,列名=表达式 where 条件;
没有where更新所有行,表达式可以是包含列名的数学表达式。
注意:空用is null
delete :
delete from 表名 where 条件;
只能按行删除,不能只删除某列,可以使用update。
可以找回数据:
savepoint aa;
delete ......
rollback to aa;
truncate table 表名; 速度快,无法回滚。
select:
(1)where 语句 to_char(date,'yyyy-mm-dd')>'1988-11-21';
to_char转换日期格式。
to_char(date,'yyyy'); 只转年。
AVG忽略空值,可以用sum/count得平均。
count(*)统计所有
count(字段)统计非空
(2)子查询
Oracle的分页查询要使用到子查询,
select job,name from EMP where sal =(select Max(sal) from EMP );
oracle 默认是从右到左执行的。
(3)分组
select deptno,AVG(sal) from MEP group by deptno having AVG(sal)<2000;
顺序:group by ... having .....order by....;
在选择列中有列、表达式和分组函数,则列和表达式至少有一个出现在group by中,依查询的实际意义而定。
(4)连接查询
在连接查询时可以为表使用别名,
等值连接,非等值连接,自连接,外连接。
补:删除用户但保留其数据对象,
alter user scott account lock;
解锁:alter user scott account unlock;