insert text exI(PID, text field) values(104 replicate(我们己取得了雅典 奥运会的的全面胜利!,100) select from text exI 例2: create table text ex2(PId int not null, name char(8), image field lage) insert text ex2(PID, image field) values(104, 0x4ab5435bf556d) select from text ex2 (四)、时间戳型 如果在表中定义了时间戳型的字段,则无论何时添加记录、修改记 录,系统都会向该字段中加入一个计数器值或修改计数器值。 时间戳字段的值为二进制格式的数据,与系统时间没有关系, 它表示对该记录的操作顺序。占8 Bytes 个表中只能有一个时间戳字段。在数据库中,该值保持唯 时间戳字段的值由系统自动确定,不允许用户修改或设置。用 Insert插入数据时,除该字段外的字段需列出,不能全部省略。 时间戳字段可以用于防止多个用户同时修改表的同一记录 例 create table timestamp ex( PID int, name char(8), tis timestamp) insert timestamp ex(PID, name) values(101, cbf1") insert timestamp ex(PID, name) values( 102, cbf2") select from timestamp ex 例2: update timestamp ex
6 insert text_ex1(PID,text_field) values(104,replicate('我们已取得了雅典 奥运会的的全面胜利!',100)) go select * from text_ex1 例 2: create table text_ex2(PID int not null,name char(8),image_field image) insert text_ex2(PID,image_field) values(104,0x4ab5435bf556d) go select * from text_ex2 (四)、时间戳型 如果在表中定义了时间戳型的字段,则无论何时添加记录、修改记 录,系统都会向该字段中加入一个计数器值或修改计数器值。 ⚫ 时间戳字段的值为二进制格式的数据,与系统时间没有关系, 它表示对该记录的操作顺序。占 8Bytes。 ⚫ 一个表中只能有一个时间戳字段。在数据库中,该值保持唯一。 ⚫ 时间戳字段的值由系统自动确定,不允许用户修改或设置。用 insert 插入数据时,除该字段外的字段需列出,不能全部省略。 ⚫ 时间戳字段可以用于防止多个用户同时修改表的同一记录。 例 1: create table timestamp_ex(PID int, name char(8), tis timestamp) insert timestamp_ex(PID,name) values(101,'cbf1') insert timestamp_ex(PID,name) values(102,'cbf2') select * from timestamp_ex 例 2: update timestamp_ex
set name=cbf22 here tis=0x000010此处tis的值应为表中某个记录中的值 update timestamp ex et name=cbf22 where PID=102 and sequal(tis, OX00000000000001F5 此处“PID=102”不能少,因为 sequal要求首先定位记录 (五)、日期时间型 datetime和 smalldatetime的区别: 项目 datetime Smalldatetime 最小值 1753-1-1 900-1-1 最大值 9999-12-31 2079-6-6 精度 I min 占用空间 8Bytes 4Bytes datetime类型的数据以字符串的形式传递给服务器,服务器负责进行 转换和合法性验证。 服务器默认的日期格式为mm/ddy(mdy格式,见“区域”设置) 可以使用 SET Dateformat [ ymd语句来设置日期格式,如mdy、ymd、dmy、 ydm、myd、dym。年月日之间可以使用/、-和分隔 datetime的值四舍五入为3ms, smalldatetime的值四舍五入为 lmin。 对于没有时间的数据,默认时间为00:00:00:对于没有日期 的时间值,默认日期为1900年1月1日(不论 datetime还是 smalldatetime),但没有日期的时间毫无意义 所有小于50的年份被认为是21世纪,>=50的两位年份被认为 是20世纪。最好写4位数 例1:演示多种日期的输入格式,并显示其结果
7 set name='cbf22' where tis=0x0000000000000192 --此处 tis 的值应为表中某个记录中的值 或: update timestamp_ex set name='cbf22' where PID=102 and tsequal(tis,0x00000000000001F5) –此处“PID=102”不能少,因为 tsequal 要求首先定位记录。 (五)、日期时间型 datetime 和 smalldatetime 的区别: 项目 datetime Smalldatetime 最小值 1753-1-1 1900-1-1 最大值 9999-12-31 2079-6-6 精度 3ms 1min 占用空间 8Bytes 4Bytes datetime 类型的数据以字符串的形式传递给服务器,服务器负责进行 转换和合法性验证。 服务器默认的日期格式为 mm/dd/yy(mdy 格式,见“区域”设置)。 可以使用 SET Dateformat [ymd]语句来设置日期格式,如 mdy、ymd、dmy、 ydm、myd、dym。年月日之间可以使用/、-和.分隔。 ⚫ datetime 的值四舍五入为 3ms,smalldatetime 的值四舍五入为 1min。 ⚫ 对于没有时间的数据,默认时间为 00:00:00;对于没有日期 的时间值,默认日期为 1900 年 1 月 1 日(不论 datetime 还是 smalldatetime),但没有日期的时间毫无意义。 ⚫ 所有小于 50 的年份被认为是 21 世纪,>=50 的两位年份被认为 是 20 世纪。最好写 4 位数。 例 1:演示多种日期的输入格式,并显示其结果
Set dateformat mdy create table date ex(PId int not null date field datetime) insert date ex values(101, 2003-8-30) insert date ex values( 102, 2003/8/30) insert date ex values( 103, 2003.8.30) insert date ex values( 1033, 8.30.2003") insert date ex values( 10333, 8.2003.30) -4位年份写全时,不受mdy限制,此时可等价使用mdy、 myd、ymd insert date ex values(104, 2003-8-30 20: 30: 21) insert date ex values(105,2003-8-3020:30:21.765) insert date ex values(106, 20: 30: 21) insert date ex valuest(107,8-30-48)-因日期格式为mdy insert date ex values( 108, 8-30-56 20: 30: 21) select from date ex 例2根据日期(不含时间)进行查询。当日期字符串中不含时间时, 默认时间为00:00:00。 例3:根据日期(含时间)进行查询。 例4:根据时间(不含日期)进行查询。当日期中不含有年月日信息 时,默认为1900年1月1日。 例5:表示某一天的日期数据: date field>>=200349 )'and date field =2003-4-923:59:59999 (六)、位型 bit是SQL中支持的逻辑数据类型,用于存储0或1。 bit字段通常用于表示 ON/OFF、 TRUE/FALSE。用作状态标志 位、活动帐户指示器、项目使用标志等
8 Set dateformat mdy create table date_ex(PID int not null ,date_field datetime) insert date_ex values(101,'2003-8-30') insert date_ex values(102,'2003/8/30') insert date_ex values(103,'2003.8.30') insert date_ex values(1033,'8.30.2003') insert date_ex values(10333,'8.2003.30') --当 4 位年份写全时,不受 mdy 限制,此时可等价使用 mdy、 myd、ymd insert date_ex values(104,'2003-8-30 20:30:21') insert date_ex values(105,'2003-8-30 20:30:21.765') insert date_ex values(106,'20:30:21') insert date_ex values(107,'8-30-48') --因日期格式为 mdy insert date_ex values(108,'8-30-56 20:30:21') select * from date_ex 例 2:根据日期(不含时间)进行查询。当日期字符串中不含时间时, 默认时间为 00:00:00。 例 3:根据日期(含时间)进行查询。 例 4:根据时间(不含日期)进行查询。当日期中不含有年月日信息 时,默认为 1900 年 1 月 1 日。 例 5:表示某一天的日期数据:date_field>=’2003-4-9’ and date_field <=’2003-4-9 23:59:59.999’ (六)、位型 bit 是 SQL 中支持的逻辑数据类型,用于存储 0 或 1。 ⚫ bit 字段通常用于表示 ON/OFF、TRUE/FALSE。用作状态标志 位、活动帐户指示器、项目使用标志等
bit类型字段的长度为 I Byte数据,如果在表中包含多个bt型字 段,则它们可以共用同一字节 bit字段不要使用空值,因为毫无意义 例 create table bit ex(编号char(9) not null性别bit婚否 bit not null, 党员否bit) /*1-男0-女* insert bit ex values( 200308001, 1,0,0) insert bit ex values(200308002, 0, 1, 1) SELECt FRom bit ex (七)、数值数据类型 整数类int、 smallint、 tinyint、 bigint 浮点类 float、real ●精确数值 numerIc、 decimal 货币类 money、 1.整数类型 用于存储精确的整数数值,其存储结构效率高,并且在本地进行处 理,在各种平台上处理速度均较快,使用较多。四种整数类型的比较: 类型 最小值 最大值 占用空间 Bigint 263-1 8Bytes 231(约21亿)|23-1(约21亿)|4Byte 215(约-3万) smallint 215-1(约3万)2 Bytes 255 例 create table int ex(编号 int identity((1,3),姓名char(8),工资 tinyint) insert int ex values张三,1200,28) ex valuest(张四980,33) insert int ex values张五,1800,24)
9 ⚫ bit 类型字段的长度为 1Byte 数据,如果在表中包含多个 bit 型字 段,则它们可以共用同一字节。 ⚫ bit 字段不要使用空值,因为毫无意义。 例: create table bit_ex(编号 char(9) not null,性别 bit,婚否 bit not null , 党员否 bit) /*1-男 0-女*/ insert bit_ex values('200308001',1,0,0) insert bit_ex values('200308002',0,1,1) SELECT * FROM bit_ex (七)、数值数据类型 ⚫ 整数类 int、smallint、tinyint、bigint ⚫ 浮点类 float、real ⚫ 精确数值 numeric、decimal ⚫ 货币类 money、smallmoney 1.整数类型 用于存储精确的整数数值,其存储结构效率高,并且在本地进行处 理,在各种平台上处理速度均较快,使用较多。四种整数类型的比较: 类型 最小值 最大值 占用空间 Bigint -2 63 2 63 -1 8Bytes Int -2 31(约-21 亿) 2 31 -1(约 21 亿) 4Bytes smallint -2 1 5(约-3 万) 2 15 -1(约 3 万) 2Bytes tinyint 0 255 1Bytes 例 1: create table int_ex (编号 int identity(1,3) , 姓名 char(8), 工资 smallint,年龄 tinyint) insert int_ex values('张三',1200,28) insert int_ex values('张四',980,33) insert int_ex values('张五',1800,24)
elect from int 关键字 identit为自动增量字段,类型可为整型、精确数值型(小数 位数必须为0),它用于标识该字段的值自动增加(初始种子值和增量值 会四舍五入成整数): create table int exI(编号 numeric identity(2.3,4.6),姓名char(8) 工资 smallint龄 tinyint)-初始种子值:2增量值:5 也可用 decimal insert int ex valuest(张三,1200,28) insert int ex values(张四",980,33 nsert int ex values张五,1800,24) select from int ex 2.近似值类型(浮点类型) 类型最小值 最大值 精度 占用空间 foat|-1.7E+308+1.79E+30815位(均精确)8 Bytes real-3.40E+38+340E+38|7位(均精确)|4 Bytes E+308代表10308,称为阶数,179称为尾数。在SQL中,foat real通常采用科学计数法表示。 精度,即数据的总长度,不包括符号和小数点。 用于科学计算或统计方面,因为其数值往往很大,但无需绝对 的精度 例 create table real ex(编号 int identity((2,4),姓名char(8),工资rel) insert real ex values(张三,1200.82673)-注意7位精度的处理 insert real ex values'张四,980) insert real ex values(张五’,1800.95) select from real ex 3.精确数值类型
10 select * from int_ex 关键字 identity 为自动增量字段,类型可为整型、精确数值型(小数 位数必须为 0),它用于标识该字段的值自动增加(初始种子值和增量值 会四舍五入成整数): create table int_ex1(编号 numeric identity(2.3,4.6) , 姓名 char(8), 工资 smallint,年龄 tinyint) --初始种子值:2 增量值:5; 也可用 decimal insert int_ex values('张三',1200,28) insert int_ex values('张四',980,33) insert int_ex values('张五',1800,24) select * from int_ex1 2.近似值类型(浮点类型) 类型 最小值 最大值 精度 占用空间 float -1.79E+308 +1.79E+308 15 位(均精确) 8Bytes real -3.40E+38 +3.40E+38 7 位(均精确) 4Bytes ⚫ E+308 代表 10308,称为阶数,1.79 称为尾数。在 SQL 中,float、 real 通常采用科学计数法表示。 ⚫ 精度,即数据的总长度,不包括符号和小数点。 ⚫ 用于科学计算或统计方面,因为其数值往往很大,但无需绝对 的精度。 例: create table real_ex(编号 int identity(2,4) , 姓名 char(8), 工资 real) insert real_ex values('张三',1200.82673) --注意 7 位精度的处理 insert real_ex values('张四',980) insert real_ex values('张五',1800.95) select * from real_ex 3.精确数值类型