对数据库中列的一些基本的操作的SQL命令

 
查询 表格的 构用 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)   精确  s  大小的十 位整数,精确 p 是指全部有几个数 (digits) 大小 s 是指小数後有几位数。如果没有特 指定, 设为  p=5; s=0 
   float         32 位元的 数。
   double        64 位元的 数。
   char(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'')
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值