SQL基本操作语句

如题所述

查询语句-select * from table;
select * from table where 条件1=数值 and 条件2=数值;
select * from table where id in (select id from table);两表关联
select a.a,b.b,c.c from table1 a,table2 b,table3 c where a.id1=b.id2;
插入语句-insert into table (字段1,字段2,字段3,……)
values (数值1,数值2,数值3,……);
更新语句-update 表名 set 数值 where=id = 1;

添加列语句-alter table 表名
add (列名1 类型1,列名2 类型2,列名3 类型3,……);

查询随机20条记录-select * from( select * from emp order by dbms_random.value) where rownum <= 10;
修改列类型-alter table 表名
modify (列名1 类型1,列名2 类型2,列名3 类型3,……);
删除列语句-alter table 表名
drop column 列名s;
显示查询时间-set timing on;

删除表语句-deltet table 表名;

清空表数据-truncate table 表名;

修改列名 - ALTER TABLE emp RENAME COLUMN comm TO newa;

集合查询(无重复):select * from table_name union
select * from table_name;
集合查询(有重复):select * from table_name union all
select * from table_name;
差 集 查 询:select * from table_name minus
select * from table_name;

--------------------------------------------------------------------------------
运行脚本-start d:\文件名.sql;

编辑脚本-edit d:\文件名.sql;

另存为脚本-spool d:\文件.sql;
select * from emp;
spool off;

分页显示-set pagesize 页数;

行数显示-set linesize 行数;

创建用户-create user 用户名 identified by 密码;(需要SYS/SYSTEM权限才能建立用户)
赋予权限-grant resource to 用户名;(建表权限)
赋予查询权限-grant select on emp to 用户名;
赋予修改权限-grant update on emp to 用户名;
赋予所有访问权限-grant all on emp to 用户名;
--------------------------------------------------------
收回查询权限-revoke select on emp from 用户名;
传递权限-grant select on emp to 用户名2 with grant option;
账户锁定-
creata profile 名称 limit failed_login_attcmpts 输入次数限制 password_lock_time 锁定天数;
------------------------------DBA权限登录
alter user 想要锁定的用户名 profile 名称;
------------------------------DBA权限登录
解锁用户锁定-alter user 用户名 account unlock;
定期修改密码-create profile 名字 limit password_life_time 天数 password_grace_time 宽限天数;

切换用户-conn system/密码;
更改密码-password 用户名;
删除用户-drop user 用户名 cascade(删除用户及用户建立的所有表);

查询同样结构两表中的不同数据-select * from emp_tmp where empno not in(select empno from emp);

select * from v$session;
select * from v$version;

定义函数:
---------函数说明 函数是计算数字平方;
FUNCTION y2
(inx2 number)
return number is
Result number(2);
begin

Result := inx2*inx2;

return(Result);
end y2;

---------函数说明 函数是输入汉字然后输出拼音;
FUNCTION HZ
(inputStr in VARCHAR2)
RETURN VARCHAR2 iS
outputStr varchar2(10);
BEGIN
SELECT c_spell INTO outputStr FROM BASE$CHINESE WHERE C_WORD = inputStr;
RETURN outputStr;
END hz;
----------函数说明 函数是计算累加自然月;
FUNCTION month
(inmonth number,
inaddmonth number)
return varchar2 is
Result varchar2(6);
begin

Result :=substr(to_char(add_months(to_date(inmonth,'yyyymm'),inaddmonth),'yyyymmdd'),1,6);

return(Result);
end month;

select to_char(add_months(trunc(sysdate),-1),'yyyymmdd') from dual;--取上个月的日期;
select to_char((sysdate-30),'yyyymmdd') from dual; ---去当前日期前30天日期;

ORACLE 随机数
DBMS_RANDOM.VALUE(low IN NUMBER,high IN NUMBER) RETURN NUMBER;
select round(dbms_random.value(x,x)) from dual;

ORACLE 取当前时间并按毫秒计算
select systimestamp from dual;

select * from cda_datasource---中继表
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-29
无非就是增删改查

insert into tablename (key1, key2 ……) values ('value1', 'value2'……)

delete from tablename where condition

update tablename set key1='value1', key2='value2'

select filed from tablename where condition
第2个回答  2011-07-29
这个问的还宽泛了,搜索SQL基础教材基本上都有
相似回答