SQL数据库 基本的操作语句(增、删、改、查)

作者&投稿:茌胖 (若有异议请与网页底部的电邮联系)
用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---中继表

例子
create table useInfo --创建用户表
(
useId int identity(1,1) priamry key not null, --用户Id,设为主键,标识列,不为空。
useName varchar(10) not null ,--用户名称,不为空
useSex char(2) not null --用户性别,不为空
)
go --批处理

insert into useInfo(useName,useSex) values('小明','男') --想表useInfo里添加信息,Id是标识列,所以不用自己手动添加,它会自动添加,所以只需添加姓名和性别。
update useInfo set useName = '大明' where useName = '小明' --把表里名字叫小明的人名字改为大明
delete from useInfo where useName = '大明' --根据姓名删除表里的信息。
truncate table useInfo --把表里的所有信息都删了,而且把标志列的数清零。如果用delete删除表里的信息 则不会把标识列清零。
select * from useInfo --查询useInfo表里的所有信息 * 表示所有
select 1 from useInfo where useName = '大明' --按条件查询,查询表里名字叫大明的人的信息

create table Student( id int primary key, name varchar(20),sex varchar(2));
insert into Student values( 1,"admin","男");
delete from Student where id=1;
update Student set name="sa" where id=1;
select id, name, sex from Student;

insert into table values()
drop from table where 。。。
update table set 字段=‘’ where 。。。
select