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

本文详细介绍了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-bytesn不能超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介于xxyy
例: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'')
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值