SYBASE数据库建表及插数据

SYBASE数据库建表及插数据

一、建表

 CREATE TABLE  SYBASE表名(

    T_ID             numeric(20) not null, 

    T_BIGINT      bigintnull,

    T_UNSIGNED_BIGINT  unsigned bigintnull,

    T_DECIMAL   decimal(10,5) null,

    T_MONEY    money null,

    T_SMALLMONEY smallmoney null,

    T_CHAR        char(20) null,

    T_UNICHAR  unichar(20)null,

    T_UNIVARCHAR  univarchar(20) null,

    T_SMALLDATETIME   smalldatetime null,

    T_TEXT    text null,

    T_UNITEXT  unitext null,

    T_IMAGE image null,

    T_BINARY binary(200) null,

    T_VARBINARY varbinary(200) null,

    T_BIT bit

);

ALTER TABLE SYBASE表名 ADD CONSTRAINT PK_SYBASE表名 primary key(T_ID);

二、插入数据

DECLARE @a int

DECLARE @b int 

SET @a = 0 while @a<1

BEGIN

WAITFOR DELAY '00:00:01'

SET @b=1 while @b<=100

BEGIN

INSERT INTO SYBASE表名 VALUES(@b+@a*100,

-1,333,123.45678,

¥123.89,¥45.67,

'zhongguo','zhengjiang','hangzhou',

getdate(),

'abcd中国浙江','大事发生大幅发发dfdf',

'image断电断电',

'0xabcd','0xfde',0)

set @b = @b+1

end

set @a = @a+1

end

三、字段类型

字段类型 | 定义 | 唯一值 |  非唯一值

------整数------------------

bigint | bigint |@b+@a*100| -111

unsigned bigint  |unsigned bigint@b+@a*100 | 111

--------字符---------------------------

char | char(20) | convert(char(20),@b+@a*100) | 'nihao'

varchar | varchar(20) |convert(varchar(20),@b+@a*100)| 'zhongguo'

unichar | unichar(20) | - | 'nihao'

univarchar | univarchar(20) | - | 'zhongguo' 

-------------------------------------------

decimal  | decimal(10,5) | @b+@a*100  | 123.45678

----------------------------------------------------

bit | bit | - | 0 / 1

--------------------------------------------------

money | money | - | ¥123.34

smallmoney | smallmoney | - | ¥123.45

-------大字段类-------------------------------

binary | binary(200) | - | '0xabcd'

varbinary | varbinary(200) | - | '0xfde'

image | image| - | 'image断电断电'

text | text | - | 'abcd中国浙江'

unitext | unitext | - |'大事发生大幅发发dfdf'

---------时间类-------------------

date | date | dateadd(day,@b+@a*100,getdate()) | '2018/01/08' /getdate()/convert(char,getdate(),102)

datetime | datetime | dateadd(day,@b+@a*100,getdate()) '2018-01-08 01:01:01.500am'/ '2018/01/01 01:01:01.287am'/ getdate()

smalldatetime | smalldatetime | dateadd(day,@b+@a*100,getdate())'2018-01-08 01:01:01.500am'/ '2018/01/01 01:01:01.287am'/ getdate()

time | time | dateadd(minute,@b+@a*100,'2018/01/01 00:00:00') | '05:06:08'/getdate() /convert(char,getdate().108) 

注:@a、@b为自定义循环变量,以便重复插值; - 表示省略; / 表示有多种可选值

选一个举例说明:bigint | bigint | @b+@a*100| -111

建表时bigint字段类型可定义为bigint; 如果该字段为主键,循环插数据时使用@b+@a*100赋值;该字段不为主键,循环插数据时使用-111赋值。

四、指定主键字段

建表时指定主键字段的三种方法:

第一种:

 CREATE TABLE  SYBASE表名(

     T_ID       bigint  primary key not null,

     T_DATE    date);

第二种:

 CREATE TABLE  SYBASE表名(

    T_ID         bigint not null, 

    T_DATE    date,

    CONSTRAINT  PK_SYBASE表名  PRIMARY KEY (T_ID)

);

第三种:

 CREATE TABLE  SYBASE表名(

    T_ID       bigint  not null,

    T_DATE    date);

ALTER TABLESYBASE表名 ADD CONSTRAINT  PK_SYBASE表名  PRIMARY KEY (T_ID);

ALTER TABLESYBASE表名 activate not logged initially;

注:PK_SYBASE表名为约束名

      创建联合主键时,在第2和第3种方法中的括号里直接加入需要增加的字段,用逗号隔开。

如:

CONSTRAINT  PK_SYBASE表名 PRIMARY KEY (T_ID,T_DATE)

ALTER TABLE SYBASE表名 ADD CONSTRAINT PK_SYBASE表名  PRIMARY KEY (T_ID,T_DATE);

五、其它

bit字段类型不允许为null

六、操作软件

 Navicat Premium软件

你可能感兴趣的:(数据库)