Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1569767
  • 博文数量: 201
  • 博客积分: 2812
  • 博客等级: 少校
  • 技术积分: 3029
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-18 18:28
个人简介

从事数据库工作多年,目前看好分布式NeSQL/HTAP数据库在企业客户市场的发展。未来的主要方向是——致力于 NewSQL/HTAP 数据库的推广普及。

文章存档

2016年(1)

2015年(8)

2014年(23)

2013年(50)

2012年(32)

2011年(87)

分类: Sybase

2011-02-05 14:52:31

     先前的几篇文章介绍了如何构建一个学习用demo数据库,在随后的文章中将向IQ的初学者介绍一些基本使用知识,这些知识适合于新接触IQ的开发人员和数据库管理人员。
 
1. 创建表
 
--使用create table语句
create table test_service
(
    service_key          int      not null,
    call_waiting_flag    char(1)  null    ,
    caller_id_flag       char(1)  null    ,
    voice_mail_flag      char(1)  null    ,
    cellular_flag        char(1)  null    ,
    internet_flag        char(1)  null    ,
    primary key (service_key)
)
注意:IQ建表语法与很多关系型数据库(例如:Sybase ASE)的语法基本相同。如果字段的空值属性没有指定的话,缺省是NULL。
 
--使用select into建表(不包含数据)
select * into test_service from service where 1=2
注意:这种方法建立的表不会自动建立索引。
 
 
2.修改表定义

(1)增加新字段
     alter table test_service add isdn_flag char(1)  null
 注意:增加的字段必须是允许NULL的。
 
(2)删除字段
     alter table test_service drop cellular_flag
 
(3)字段改名
     alter table test_service rename call_waiting_flag to wait_flag

(4)增加主键约束
     alter table test_service add primary key(service_key)
     注意:主键字段一定是NOT NULL的
 
(5)删除主键约束
     alter table test_service drop primary key
 
(6)修改表名
     alter table service rename service1
 
 (7) 修改字段数据类型
     IQ不支持直接修改表中某个字段的数据类型。如果想这样做,可以采用间接方法:
     a.增加一个新字段(这个字段的类型是你期望的数据类型)
     b.使用update语句把源字段的值写入新字段(可能需要使用类型转换函数)
     c.把源字段删除
     d.把新增加的字段改为与被删除那个字段的名相同
 
     下面是一个例子,把test1表的age字段类型从int改为tinyint :
    
     --准备表和数据
     create table test1(id int,name char(8),age int)
     insert into test1 values(1,'aaaa',20)
     insert into test1 values(2,'bbbb',30)
     commit
 
     --a.增加一个新字段
     alter table test1 add age1 tinyint null
 
     --b.使用update语句把源字段的值写入新字段
     update test1 set age1 = age
 
     --c.把源字段删除
     alter table test1 drop age
 
     --d.把新增加的字段改为与被删除那个字段的名相同
     alter table test1 rename age1 to age
 
     还有另一种方法,步骤如下:
     --a. 使用select into把表数据进行备份
     select * into test1_tmp from test1;
 
     --b. 删除表
     drop table test1
    
     --c. 用正确的类型重新创建表
     create table test1(id int,name char(8),age tinyint)
 
     --d. 把数据导回表
     insert test select * from test1_tmp
阅读(5211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~