数据库设计源代码.docx
《数据库设计源代码.docx》由会员分享,可在线阅读,更多相关《数据库设计源代码.docx(73页珍藏版)》请在三一办公上搜索。
1、/选择一种高级语言实现下列语句的功能。/CREATETABLE表名(V列名数据类型歹IJ完整性约束条件习,列名数据/类型列完整性约束条件习ILV表完整性约束条件)/Altertable表名addV新列名数据类型列完整性约束DROPv列完整性约束名习MODIFYV列名V数据类型运用说明1、将程序文件table.sql放在D盘根书目下。2、在C盘根书目下建立一个名为“数据库”的文件夹,用于存储表。3、建立的表存储路径为C:数据库:table.dbf。4、在程序文件table.sql中只有一条建表语句和三条修改表语句,在以程序方式执/行时留意执行的次数,慎重选择“是否接着执行”。5、程序输入的SQL
2、语句格式如下:/createtablestudent/(/SNOintPRIMARYKEY,/SNAMEchar(10)UNIQUE,/SAGEint,/SDEPTchar(20)NOTNULL,/COURSEchar(20),/GRADEint/);/altertablestudentaddCNOintNOTNULL;/altertablestudentaltercolumnSAGEshort;/altertablestudentdropSDEPT;#include#include#include#include/宏定义#defineYEARO#defineMONTH1#defineDAY2
3、#defineFOX_VERISONNFo262#defineMAX40/字段类型#defineDATE0x44#defineDOUBLE0x45#defineFLOAT0x46#defineSHORT0x47#defineINT0x48#defineTRUE1#defineFALSEO/文件头结构体定义structDbfHead(chardbFlag;charyear;charmonth;charday;intrecCounts;shortfirstRecAddr;shortrecLen;charundo20;);*pDbfHead;typedefstructDbfHeadDbfHead,/
4、字段描述结构体定义structFieldDcp(charfieldNamel0;charundo1;charfieldType;shortoffset;charundo22;charfieldLen;charnumDecis;charundo314;);typedefstructFieldDcpFieldDcp,*pFieldDcp;/字段数据结构体定义structDbfFieldchar*fieldData;charfieldNamel0;charfieldType;charfieldLen;charfieldDcis;shortoffset;);typedefstructDbfFieldD
5、bfField,*pDbfField;记录结构体定义structDbfRec(chardelFlag;DbfFieldfieldMAX;intrealCounts;typedefstructDbfRecDbfRec,*pDbfRec;dbf文件句柄定义structDbfHandchar50;DbfHeadheader;DbfRecrec;longcurRecNo;longCurFpAddr;FILE*fd;);typedefstructDbfHandDbfHand,*pDbfHand;约束条件typedefstructConditionintflag;/flag用于区分约束条件(PARIMAR
6、YKEY1UNIQUE2NOTNULL3)Condition;pDbfHandf;DbfRecrec;chartable_name20;charsql300,sql1300;charGetDate(intgetMode)/获得日期(if(DAY=getMode)return01;if(MONTH=getMode)return06;if(YEAR=getMode)return10;elseexit(0);shortGetHeadLength(pDbfRecrec)获得文件头长度(returnrec-realCounts*32+32+2;shortGetRecLength(pDbfRecrec)/
7、获得文件体长度(registerinti=0;intcount=0;for(i=0;irealCounts;i+)if(rec-fieldi.fieldType=,D,)count+=8;continue;elseif(rec-fieldi.fieldType=,)count+=4;continue;elseif(rec-fieldi.fieldType=,T,)count+=6;continue;count+=rec-fieldi.fieldLen;returncount+1;)pDbfHandNewDbfHead(pDbfHandhand,pDbfRecrec)/新建文件intret=-1
8、;hand-header.dbFlag=0x03;hand-header.day=GetDate(DAY);hand-header.month=GetDate(MONTH);hand-header.year=GetDate(YEAR);hand-header.recLen=GetRecLength(rec);hand-header.recCounts=O;hand-header.firstRecAddr=GetHeadLength(rec);if(O!=fseek(hand-fd,O,SEEK.SET)returnNULL;ret=fwrite(char*)&hand-header,Sizeo
9、f(DbfHead),1hand-fd);if(ret!=-1)returnhand;elsereturnNULL;)pDbfHandUpdateHead(pDbfHandhand)/更新文件头(if(O!=fseek(hand-fd,O,SEEK.SET)returnNULL;if(-1=fwrite(char*)&hand-header,Sizeof(DbfHead),1,hand-fd)returnNULL;fclose(hand-fd);returnhand;)pDbfHandWriteFieldDicsribe(pDbfHandhand,pDbfRecrec)/写字段描述部分inti
10、=0;FieldDcpfield=O;field,offset=0x01;for(i=0;irealCounts;i+)(field.fieldType=rec-fieldi.fieldType;field.numDecis=0;switch(field.fieldType)caseDOUBLE:field.fieldLen=8;break;caseFLOAT:field.fieldLen=4;break;caseINT:field.fieldLen=4;break;caseSHORT:field.fieldLen=2;break;default:field.fieldLen=rec-fiel
11、di.fieldLen;break;strcpy(field.fieldName,rec-fieldi.fieldName);field.offset+=(short)rec-fieldi.fieldLen;if(-l=fwrite(char*)sfield,Sizeof(FieldDcp),1,hand-fd)returnNULL;hand-rec.delFlag=0x20;hand-rec.realCounts=rec-realCounts;for(i=0;irealCounts;i+)hand-rec.fieldi=rec-fieldi;returnhand;voidWriteField
12、End(pDbfHandhand)/写文件头结束标记inti=0;charbuf2=OxOD,OxOO;charversionBufFOX_VERISONJNFO=0;if(0=fseek(hand-fd,0,SEEK.END)(if(-1=fwrite(buf,2,1,hand-fd)return;return;pDbfHandReadDbfHead(pDbfHandhand)读文件头信息inti=0,j=0;if(-1!=(fseek(hand-fd,0,SEEK.SET)(if(0!=fread(char*)&hand-header,32,1,hand-fd)returnhand;ret
13、urnNULL;intGetFieldCount(pDbfHandhand)/得至字段个数inti=0;intoffset=1;FieldDcpfield=0;charcEnd=0;for(i=O;ifd,(i+1)*32,SEEK.SET)return-1;if(O!=fread(char*)&field,32,1,hand-fd)fread(&cEnd,l,1,hand-fd);if(OxOD=cEnd)return(i+1);return-1;pDbfHandReadFieldDiscribe(pDbfHandhand)/读字段描述信息inti=0,j=0;intfieldCount=G
14、etFieldCount(hand);hand-rec.realCounts=fieldCount;for(i=0;irec.realCounts;i+)(FieldDcpfield=0;if(-1=fseek(hand-fd,(i+l)*32,SEEK.SET)returnNULL;if(-1=fread(char*)&field,32,1,hand-fd)returnNULL;memcpy(hand-rec.fieldi.fieldName,field.fieldName,sizeof(field.fieldName);hand-rec.fieldi.fieldLen=field.fiel
15、dLen;hand-rec.fieldi.fieldType=field.fieldType;hand-rec.fieldi.fieldDcis=field.numDecis;hand-rec.fieldi.offset=field.offset;for(i=0;irec.realCounts;i+)for(j=Ojhand-rec.fieldi.fieldNamej!=,0,d+)hand-rec.fieldi.fieldNamej=tolower(hand-rec.fieldi.fieldNamej);returnhand;char*SaveDate(Char*str)保存内容为日期时,格
16、式华inti=0,j=0;chartemp9=0;if(strlen(str)!=10)日期格式输入不合法returnNULL;for(i=0,j=0;i=,0,&*(str+i)rec.realCounts;buf=(char*)malloc(hand-header.recLen+1);memset(buf,0x20,hand-header.recLen1);bufO=,;for(i=O;irec.fieldi.fieldType=DATE&record-fieldi.fieldData!=NULL)record-fieldi.fieldData=SaveDate(record-fieldi
17、.fieldData);CurFieldLen=hand-rec.fieldi.fieldLen;actDataLen=strlen(record-fieldi.fieldData);if(actDataLenCurFieldLen)actDataLen=CurFieldLen;memcpy(&bufnPos,record-fieldi.fieldData,actDataLen);nPos+=hand-rec.fieldi.fieldLen;bufhand-header.recLen=,0;if(-1=fwrite(buf,hand-header.recLen,1,hand-fd)rest=F
18、ALSE;returnrest;char*ReadDbfDate(char*str)读日期时,格式化inti=0,j=O;chartemp11=0;char*strRest=NULL;for(i=0,j=0;i=0;i)if(*(str+i)!=0x20)break;stri=0x00;returnstr;*pDbfFieldGetCurrentField(pDbfHandhandle,pDbfRecrec,intfieldld)/得到当前文件(return(PDbfField)&(rec-fieldfieldld);)7intGetFieldNum(pDbfHandhand,char*fie
19、ld_name)/得到文件个数intfIag=O;intj;for(j=0;jrec.realCounts;j+)if(O=strcmp(field_name,hand-rec.fieldj.fieldName)flag=l;break;if(flag)returnj;elsereturn-1;)intIsBottomRecord(pDbfHandhand)推断是否是第一条记录(intrecCounts=hand-header.recCounts;if(hand-curRecNo!=hand-header.recCounts)returnFALSE;returnTRUE;intGotoTop(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 设计 源代码
链接地址:https://www.31ppt.com/p-5940064.html