经典SQL(2)
1、经典的查询语句
2、经典的字定义函数
3、经典的与业务相关的存储过程
等等
1、 跟踪当前对话下用户的SQL脚本
select sql_text from v$sqltext_with_newlines where (hash_value,address)
in (select sql_hash_value,sql_address from v$session where sid=&sid)
order by address,piece;
SID
由这得到
select sid,machine from v$session;
======
desc table;检查表结构
select * from tab where tabtype='TABLE';显示当前用户下的所有表。
select count(*) from table;显示此表的数据行数;
spool c:\tony.txt;日记路径
spool off;关闭记录后可以看到日记文件里的内容。
alter table stu add(classid number(2));添加字段
alter table stu modify(xm varchar2(12));修改字段的长度
alter table stu drop column sal;
drop table stu;
rename student to stu;
alter table student drop column sal; alter table stu add(salary number(7,2));
insert into stu values('A001','张三','男','01-5月-05',10);
insert into stu(xh,xm,sex) values ('A003','JOHN','女');
insert into student(xh,xm,sex,birthday) values ('A004','MARTIN','男',null);
修改
update
update stu set sex='女' where xh='A001';
update student set sex='男',birthday='1980-04-01'where xh='A001';
update student set classid=20 where birthday is null;
delete from stu;drop table student;delete from stu where xh='A001';
truncate table stu;删除表中的所有记录,表结构还在不写日记无法找回记录
select * from stu;
select * from student where classid like '1%';
select * from student where xh like '%A%';
select * from student where xh like 'A%';
select * from student where xh like '%A';
select * from student where xh = 'A%';
select * from student order by birthday;
select * from student order by birthday desc,xh asc; --按birthday 降序 按xh升序(asc/默认)
select * from student where sex='女' or birthday='1999-02-01';
select * from student where sex='女' and birthday='1999-02-01';
select * from student where salary > 20 and xh <> 'B002'; (!=)
oracle
函数的学习
单行函数 返回值只有一个
分组
相关文档:
如果你经常遇到下面的问题,你就要考虑使用SQL Server的模板来写规范的SQL语句了:
SQL初学者。
经常忘记常用的DML或是DDL SQL 语句。
在多人开发维护的SQL中,每个人都有自己的SQL习惯,没有一套统一的规范。
在SQL Server Management Studio中,已经给大家提供了很多常用的现成SQL规范模板。
SQL Server Management ......
想起来上次面试的时候那个面试官问我
“你对优化方面了解多少”
“ORACLE优化还是SQL优化”
“SQL优化”
“这个……不知道从何说起”
“呵呵,那我问你问题好了,问问就知道你大概什么水平了”
“呵呵,好吧”
……
结果好像 ......
/*
PL/SQL表---table()函数用法:
利用table()函数,我们可以将PL/SQL返回的结果集代替table。
oracle内存表在查询和报表的时候用的比较多,它的速度相对物理表要快几十倍。
simple example:
1、table()结合数组:
*/
create or replace type t_test as object(
id integer,
rq date,
mc varchar2(60)
);
cr ......
IF EXISTS (SELECT name from master.dbo.sysdatabases WHERE name = N'MyDatabase')
DECLARE @backupdate varchar(255);
DECLARE @result int
DECLARE @result1 int
DECLARE @dbid uniqueidentifier
SET @dbid = NEWID()
EXEC @result = xp_cmdshell 'cd D:\SQLServerBackups';
IF (@result = 1)
EXEC @result1 = ......
在脑子里老是记得当初写SQL的时候,总是有人提醒对于主键的条件要写在前面,至于为什么现在总是记不清楚了。但是SQL中where 条件的执行顺序跟主键以及索引有很大的关系。
把上片中的表a 加上主键:
alter table
add constraint pk_a_id primary key (id)
然后在运行上篇中出错的例句:
select * from a where id in (1 ......