笔试SQL语句——学习笔记
定义:
create table 表名(列名1 类型 [not null] [,列名2 类型] [not null],···) [其他参数]
修改:
alter table 表名 add 列名 类型
alter table 表名 rename column 原列名 to 新列名
alter table 表名 alter column 列名 类型 [(宽度) [,小数位]]
alter table 表名 drop column 列名
删除:
drop table 表名
建立列的索引:
create [unique] index 索引名 on 基本表名(列名 [次序] [,列名 [次序]] ···) [其他参数]
其中的次序,ASC(升序,缺省) DESC(降序)
drop index
select 目标列 from 表(视图) [where 条件表达式] [group by 列名1] [having 内部函数表达式] [order by 列名 [ASC|DESC]]
若不同表中的列同名,则写为“表名.列名”
其中的目标列可使用以下函数:
count(列名|*),sum(列名),avg(列名),max(列名),min(列名)
记录唯一:distinct 列名1 [,列名2···]
“*”表示任意字符串 “?”表示任意字符
几个例子:
从学生表中查询班级:select distinct 班级 from 学生
先按班级,再按学号排序:select * from 学生 order by 班级,学号、
select * from 学生 where 条件1 and 条件2 and sth IS [NOT] NULL
where 班级 [NOT] in (’0001‘,’0002‘)等价于 where 班级 =’200101‘ or 班级 =’200202‘
where 出生年份 between 1982 and 1990
查询2001级的学生:
select * from 学生 where 班级 like ’2001%‘
_(下横线)表示任意单个字符
%表示任意字符串
查询课程超过三门的学生:
select 学号 from 成绩单 group by 学号 having count(*)>3
自动连接:
select 学生.*,成绩.* from 学生,成绩 where 学生.学号=成绩.学号 order by 课程号,分数 DESC
等价于 select 学生.*,课程号,分数 order by 课程号,分数 DESC
嵌套查询:
select 姓名 from 学生 where 学号 in (select 学号 from 成绩 where 课程号=’C1‘)
求两表的交集或差集(字段相同):
select * from 成绩1 where 学号 [NOT] in (select 学号 from 成绩2 where 成绩1.课程号=成绩2.课程号 and 成绩1.分数=成绩2.分数)
UNION连接:
select 列名1 from 表1 where 条件1 UNION select 列名2 from 表2 where 条件2
相关文档:
(1) Connect to the Analysis server, select the database which we want it to be automatically processed. Right click on this database, choose ‘Process’:
(2) In the opening ‘Process database’ form, click the ‘Script Action ......
--设置数据库输出,默认为关闭,每次新打开窗口都要重新设置
set serveroutput on
--调用 包 函数 参数
execute dbms_output.put_line('hello world');
--或者用call调用,相当于java中的调试程序打桩
call d ......
刚刚安装的数据库系统,按照默认安装的话,很可能在进行远程连接时报错,通常是错误:"在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) "搜MSDN,上面有一片机器翻译的文章,是在让人难以明白,现在 ......