查询
表格的
结
构用
desc table_name;
一、 data definition language
1 、建表格
create table table_name(column1 datatype [not null] [not null primary key], column2 datatype [not null],..)
datatype - 资 料的格式
not null - 不可以允 许资 料有空的
primary key - 是本表的主 键
实 例: create table randy (name char(10),age integer);
2 、更改表格
alter table table_name add column column_name datatype
增加一个 栏 位(没有 删 除某个 栏 位的 语 法)
实 例: alter table randy add address varchar(100); -- 在 实际应 用中不能有 column 关键 字
alter table table_name add primary key (column_name)
更改表的定 义 把某个 栏 位 设为 主 键
实 例: alter table randy add primary key (name);
alter table table_name drop primary key (column_name)
把主 键 的定 义删 除
实 例: alter table randy drop primary key; -- 实际应 用中不需要 column_name
3 、建立索引
create index index_name on table_name (column_name)
说 明 : 对 某个表格的 栏 位建立索引可以增加 查询时 的速度
实 例: create index randyindex on randy (name);
4 、 删 除
删 除表 drop table_name
删 除索引 drop index_name
注意不能 删 除表中的某个 栏 位
实 例: drop table table_name;
drop index index_name; -- 删 除 时 需要指明 table 或 index 关键 字
二、 资 料形 态 datatypes
smallint 16 位元的整数。
interger 32 位元的整数。
decimal(p,s) p 精确 值 和 s 大小的十 进 位整数,精确 值 p 是指全部有几个数 (digits) 大小 值 , s 是指小数後有几位数。如果没有特 别 指定, 则 系 统 会 设为 p=5; s=0 。
float 32 位元的 实 数。
double 64 位元的 实 数。
char(n) n 长 度的字串, n 不能超 过 254 。
varchar(n) 长 度不固定且其最大 长 度 为 n 的字串, n 不能超 过 4000 。
graphic(n) 和 char(n) 一 样 ,不 过 其 单 位是两个字元 double-bytes , n 不能超 过 127 。 这 个形 态 是 为 支援两个字元 长 度的字体,例如中文字。
vargraphic(n) 可 变长 度且其最大 长 度 为 n 的双字元字串, n 不能超 过 2000 。
date 包含了年份、月份、日期。
time 包含了 小 时 、分 钟 、秒。
timestamp 包含了年、月、日、 时 、分、秒、千分之一秒。
三、 资 料操作 data manipulation language
1 、增加 资 料
insert into table_name (column1,column2,...) values (value1,value2,...)
实 例: insert into randy (name,age) values (''l'',16); -- 字符串要用 '' '' 表示。
insert into randy values (''wei'',26,''xiddian''); -- 没有指定 column 则 会按照 栏 位 顺 序填入 资 料
注意 table_name 也可以是景 观 view_name;
insert into table_name (column1,column2,...) select column1x,column2x,... from another_table
说 明:也可以 经过 一个子 查询 把 别 的表格的 资 料填入
实 例: insert into randy select * from copyrandy;
2 、 查询资 料
select column1,column2 from table_name
select * from table_name where column1 = xxx [and column2 > yyy] [or column3 <> zzz]
实 例: select * from randy where name<>''l'' and age<27;
select * from table_name order by column2 [desc]
说 明: order by 是指定以某个 栏 位做排序, [desc] 是指从大到小排列,而默 认 是从小到大排列。
实 例: select * from randy order by age desc; --[desc] 的 [] 只是 说 明是可 选项
组 合 查询 : 联 合一个以上的表格才能 够 得到 结 果
select * from table1,table2 where table1.colunn1=table2.column1
实 例: select * from randy,copyrandy where randy.age=copyrandy.age;
整合性 查询
select count(*) from table_name where column_name = xxx
说 明:符合条的 资 料共有几笔
实 例: select count(*) from randy where age<30;
select sum(column1) from table_name
说 明: 计 算出 总 和,所 选 的 栏 位必 须 是可数的数字形 态
实 例: select sum(age) from randy;
除此以外 还 有 avg(),max(),min() 等函数。
select column1,avg(column2) from table_name group by column1 having avg(column2)>xxx
说 明: group by column 以 column1 为 一 组计 算 column2 的平均 值 ,如果 column1 只有一列数据, 则 avg(column2) 则 是 column2 本身。
having 必 须 和 group by 一起使用作 为 整合性的限制。
实 例: select name,avg(age) from randy group by name having avg(age)<27;
复 合性 查询
select * from table1_name where exists (select * from table2_name where conditions)
说 明: exists 指存在与否
实 例: select * from randy where exists (select * from copyrandy where age=26); --where 后面的部分只是一个条件 condition ,如果条件 为 真 则执 行前面的部分,前面部分 执 行的内容和条件无 关 。
select * from table_name1 where column1 in (select column1 from table_name2 where conditions)
说 明: in 后面是一个集合表示 column1 存在集合里面
select 出来的 资 料形 态 必 须 符合 column1
实 例: select * from randy where name in (select name from copyrandy);
其他 查询
select * from table_name1 where column1 like ''x%''
说 明: like 必 须 与后面的 ''x%'' 相呼 应 表示以 x 开头 的子串
实 例: select * from randy where name like ''w%'';
select * from table_name1 where column1 in (''xxx'',''yyy'',...)
说 明: in 后面是一个集合,表示 column1 在集合里面
实 例: select * from randy where name in (''ling'',''wei'',''ran'');
select * from table_name1 where column1 between xx and yy
说 明: between 表示 column1 的 值 介于 xx 与 yy 之 间
实 例: select * from randy where age between 1 and 27;
3 、更改 资 料
update table_name set column1=''xxx'' where conditions
实 例: update randy set name=''ling'' where age=16;
4 、 删 除 资 料
delete from table_name where conditions
实 例: delete from randy where name=''ling'';
日期的比 较 不同的数据 库 有不同的表达式
1 ) access 数据 库 where mydate>#2000-01-01#
2 ) Oracle 数据 库 where mydate>case(''2000-01-01'' as date) 或 where mydate>to_date(''2000-01-01'',''yyyy-mm-dd'')
一、 data definition language
1 、建表格
create table table_name(column1 datatype [not null] [not null primary key], column2 datatype [not null],..)
datatype - 资 料的格式
not null - 不可以允 许资 料有空的
primary key - 是本表的主 键
实 例: create table randy (name char(10),age integer);
2 、更改表格
alter table table_name add column column_name datatype
增加一个 栏 位(没有 删 除某个 栏 位的 语 法)
实 例: alter table randy add address varchar(100); -- 在 实际应 用中不能有 column 关键 字
alter table table_name add primary key (column_name)
更改表的定 义 把某个 栏 位 设为 主 键
实 例: alter table randy add primary key (name);
alter table table_name drop primary key (column_name)
把主 键 的定 义删 除
实 例: alter table randy drop primary key; -- 实际应 用中不需要 column_name
3 、建立索引
create index index_name on table_name (column_name)
说 明 : 对 某个表格的 栏 位建立索引可以增加 查询时 的速度
实 例: create index randyindex on randy (name);
4 、 删 除
删 除表 drop table_name
删 除索引 drop index_name
注意不能 删 除表中的某个 栏 位
实 例: drop table table_name;
drop index index_name; -- 删 除 时 需要指明 table 或 index 关键 字
二、 资 料形 态 datatypes
smallint 16 位元的整数。
interger 32 位元的整数。
decimal(p,s) p 精确 值 和 s 大小的十 进 位整数,精确 值 p 是指全部有几个数 (digits) 大小 值 , s 是指小数後有几位数。如果没有特 别 指定, 则 系 统 会 设为 p=5; s=0 。
float 32 位元的 实 数。
double 64 位元的 实 数。
char(n) n 长 度的字串, n 不能超 过 254 。
varchar(n) 长 度不固定且其最大 长 度 为 n 的字串, n 不能超 过 4000 。
graphic(n) 和 char(n) 一 样 ,不 过 其 单 位是两个字元 double-bytes , n 不能超 过 127 。 这 个形 态 是 为 支援两个字元 长 度的字体,例如中文字。
vargraphic(n) 可 变长 度且其最大 长 度 为 n 的双字元字串, n 不能超 过 2000 。
date 包含了年份、月份、日期。
time 包含了 小 时 、分 钟 、秒。
timestamp 包含了年、月、日、 时 、分、秒、千分之一秒。
三、 资 料操作 data manipulation language
1 、增加 资 料
insert into table_name (column1,column2,...) values (value1,value2,...)
实 例: insert into randy (name,age) values (''l'',16); -- 字符串要用 '' '' 表示。
insert into randy values (''wei'',26,''xiddian''); -- 没有指定 column 则 会按照 栏 位 顺 序填入 资 料
注意 table_name 也可以是景 观 view_name;
insert into table_name (column1,column2,...) select column1x,column2x,... from another_table
说 明:也可以 经过 一个子 查询 把 别 的表格的 资 料填入
实 例: insert into randy select * from copyrandy;
2 、 查询资 料
select column1,column2 from table_name
select * from table_name where column1 = xxx [and column2 > yyy] [or column3 <> zzz]
实 例: select * from randy where name<>''l'' and age<27;
select * from table_name order by column2 [desc]
说 明: order by 是指定以某个 栏 位做排序, [desc] 是指从大到小排列,而默 认 是从小到大排列。
实 例: select * from randy order by age desc; --[desc] 的 [] 只是 说 明是可 选项
组 合 查询 : 联 合一个以上的表格才能 够 得到 结 果
select * from table1,table2 where table1.colunn1=table2.column1
实 例: select * from randy,copyrandy where randy.age=copyrandy.age;
整合性 查询
select count(*) from table_name where column_name = xxx
说 明:符合条的 资 料共有几笔
实 例: select count(*) from randy where age<30;
select sum(column1) from table_name
说 明: 计 算出 总 和,所 选 的 栏 位必 须 是可数的数字形 态
实 例: select sum(age) from randy;
除此以外 还 有 avg(),max(),min() 等函数。
select column1,avg(column2) from table_name group by column1 having avg(column2)>xxx
说 明: group by column 以 column1 为 一 组计 算 column2 的平均 值 ,如果 column1 只有一列数据, 则 avg(column2) 则 是 column2 本身。
having 必 须 和 group by 一起使用作 为 整合性的限制。
实 例: select name,avg(age) from randy group by name having avg(age)<27;
复 合性 查询
select * from table1_name where exists (select * from table2_name where conditions)
说 明: exists 指存在与否
实 例: select * from randy where exists (select * from copyrandy where age=26); --where 后面的部分只是一个条件 condition ,如果条件 为 真 则执 行前面的部分,前面部分 执 行的内容和条件无 关 。
select * from table_name1 where column1 in (select column1 from table_name2 where conditions)
说 明: in 后面是一个集合表示 column1 存在集合里面
select 出来的 资 料形 态 必 须 符合 column1
实 例: select * from randy where name in (select name from copyrandy);
其他 查询
select * from table_name1 where column1 like ''x%''
说 明: like 必 须 与后面的 ''x%'' 相呼 应 表示以 x 开头 的子串
实 例: select * from randy where name like ''w%'';
select * from table_name1 where column1 in (''xxx'',''yyy'',...)
说 明: in 后面是一个集合,表示 column1 在集合里面
实 例: select * from randy where name in (''ling'',''wei'',''ran'');
select * from table_name1 where column1 between xx and yy
说 明: between 表示 column1 的 值 介于 xx 与 yy 之 间
实 例: select * from randy where age between 1 and 27;
3 、更改 资 料
update table_name set column1=''xxx'' where conditions
实 例: update randy set name=''ling'' where age=16;
4 、 删 除 资 料
delete from table_name where conditions
实 例: delete from randy where name=''ling'';
日期的比 较 不同的数据 库 有不同的表达式
1 ) access 数据 库 where mydate>#2000-01-01#
2 ) Oracle 数据 库 where mydate>case(''2000-01-01'' as date) 或 where mydate>to_date(''2000-01-01'',''yyyy-mm-dd'')