




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
數(shù)據(jù)庫課程設計設計題目:數(shù)據(jù)庫創(chuàng)建和修改表專業(yè):物聯(lián)網(wǎng)工程班級:2011級_設計人:林升_學號:201101052143山東科技大學201課程設計任務書課程設計題目:創(chuàng)建和修改表的定義課程設計主要參考資料:《數(shù)據(jù)庫系統(tǒng)概論(第四版)》,王珊、薩師煊主編,高等教育出版社《C程序設計(第三版)》,譚浩強主編,清華大學出版社課程設計應解決的主要問題:(1)實現(xiàn):CREATETABLE<表名>(<列名><數(shù)據(jù)類型>[<列完整性約束條件>][,<列名><數(shù)據(jù)類型>[<列完整性約束條件>]…][,<表完整性約束條件>]);(2)實現(xiàn):ALTERTABLE<表名>[ADD<新列名><數(shù)據(jù)類型>[<列完整性約束>]][DROP<列完整性約束名>][MODIFY<列名><數(shù)據(jù)類型>]。山東科技大學學生課程設計 第1頁設計要求:(1)設計內容創(chuàng)建和修改表的定義:1、實現(xiàn):CREATETABLE<表名>(<列名><數(shù)據(jù)類型>[<列完整性約束條件>][,<列名><數(shù)據(jù)類型>[<列完整性約束條件>]…][,<表完整性約束條件>]);2、實現(xiàn):ALTERTABLE<表名>[ADD<新列名><數(shù)據(jù)類型>[<列完整性約束>]][DROP<列完整性約束名>][MODIFY<列名><數(shù)據(jù)類型>]。(2)設計要求1、設計和實現(xiàn)表的物理存儲結構;2、語句以命令行和圖形化界面兩種形式實現(xiàn);3、分析設計內容,畫出程序流程圖,設計表的存儲結構;4、提交課程設計報告。(3)任務步驟1、分析命令語句,得到表名、列名和數(shù)據(jù)類型等信息;2、根據(jù)命令中的關鍵詞確定表和字段的屬性;3、創(chuàng)建一個表文件,寫入表結構信息;4、打開一個表文件,修改表結構信息;5、演示建立了一個表,并修改了表結構。需求分析:數(shù)據(jù)庫系統(tǒng)能夠有效地組織和管理大量的數(shù)據(jù)。研究數(shù)據(jù)庫管理系統(tǒng)的實現(xiàn)技術,對于掌握數(shù)據(jù)庫系統(tǒng)的原理和技術,了解數(shù)據(jù)庫系統(tǒng)的內部結構,開發(fā)高效的數(shù)據(jù)庫應用系統(tǒng),具有重要意義。在建立了數(shù)據(jù)庫之后,首先需要建立表,之后才能進行記錄的插入。這個程序的設計就是實現(xiàn)創(chuàng)建和修改表的定義。設計思想:(1)總體思路此課程設計主要要完成的任務是創(chuàng)建和修改表的定義,因此程序中共包含四個可選項:“新建表”、“修改表”、“顯示表的信息”、“保存操作并退出”。(2)主要結構1、每一個都有一個固定結構,因此我首先建立了一個表的結構體,具體形式如下:structField{charname[N];chartype[N];intlen;charcondition[N];Field(){len=0;name[0]=type[0]=condition[0]=0;}//構造函數(shù)};(3)讀入的語句字符串charsql[MAX],sqltmp[MAX];(4)新建表(5)修改表(6)顯示表的信息(7)保存操作并退出程序流程圖:主要源程序:(1)定義表結構體typedefstruct{ chartable_name[20];//表名 intproperty_num;//屬性的個數(shù) charproperty_name[100][20];//屬性名 intproperty_type[2];//屬性類型(1=int,2=char) intproperty_null[2];//屬性是否為空(1=允許,2=不允許) intproperty_key[2];//是否主碼(1=是主鍵,2=不是主鍵)}TABLE;(2)讀入SQL語句,并進行格式化分離單詞,以分號結束,ESC退出整個程序intread(){charc;inti;for(i=0;c=getch();i++){if(c==';')break;if(c==27)exit(0);//esc的ASC碼位27,結束程序if(c==8)//退格{i-=2;if(i<-1)i=-1;system("cls");printf("<ESCForExit,'outputtable_name'foroutputthetable>:\n請輸入SQL語句:\n\n");for(intj=0;j<=i;j++){if(sqltmp[j]==13)//回車{puts("");}printf("%c",sqltmp[j]);}continue;}sqltmp[i]=c;if(c==13){puts("");sql[i]='';}elseif(c=='('||c==')'||c=='\''||c=='\n'){printf("%c",c);sql[i]='';}elseif(c==','){printf("%c",c);sql[i]='';sql[i+1]=',';sql[i+2]='';i+=2;}else{printf("%c",c);sql[i]=c;}}sql[i]=0;printf(";");puts("");Tolower(sql);return0;}(3)向表中添加域voidadd(Fieldfield,char*table_name){if(access(table_name,0)==-1){FILE*fp=fopen(table_name,"w");fwrite(&field,sizeof(Field),1,fp);fclose(fp);}else{FILE*fp=fopen(table_name,"a");fwrite(&field,sizeof(Field),1,fp);fclose(fp);}}(4)建立表voidcreatetable(){ TABLEnewtable;//用來存儲新創(chuàng)建的表 //step1:輸入表名 inttablename_is_right=0; while(!tablename_is_right) { system("cls"); printf("表名:"); scanf("%s",&newtable.table_name); getchar(); //判斷表名是否已經(jīng)存在 intname_is_equal=0; for(inti=0;i<table_num;i++) { if(strcmp(newtable.table_name,table[i].table_name)==0) { name_is_equal=1;break; } } if(name_is_equal==1) { printf("表%s已經(jīng)存在,按任意鍵返回重新輸入",newtable.table_name); getchar(); } elsetablename_is_right=1; }//表名while //step2:輸入屬性個數(shù) intpropertynum_is_right=0; while(!propertynum_is_right) { system("cls"); printf("表名:%-10s\n",newtable.table_name); printf("屬性個數(shù):"); scanf("%d",&perty_num); getchar(); //判斷屬性個數(shù)是否正確 if(perty_num<=0) { printf("您輸入的屬性個數(shù)有誤,按任意鍵重新輸入\n"); getchar(); } elsepropertynum_is_right=1; }//屬性個數(shù)while inti;//i代表循環(huán)中的屬性 intj;//j代表在i之前的屬性for(i=0;i<perty_num;i++)//i代表屬性個數(shù)0,1,2 {//step3:輸入屬性名 intpropertyname_is_right=0; while(!propertyname_is_right) { system("cls"); printf("表名:%-10s\n",newtable.table_name); printf("屬性個數(shù):%d\n",perty_num); printf("\n"); printf("屬性名屬性類型是否允許為空是否為主鍵\n");printf("(1=int2=float)(1=是2=否)(1=是2=否)\n");printf("\n"); for(j=0;j<i;j++) { printf("屬性%2d:%-10s%d%d%d",j+1,perty_name[j],perty_type[j],perty_null[j],perty_key[j]); } printf("屬性%2d:",i+1); scanf("%s",&perty_name[i]); //判斷屬性名是否存在 intname_is_equal=0; for(j=0;j<i;j++) { if(strcmp(perty_name[i],perty_name[j])==0) name_is_equal=1; } if(name_is_equal==1) { printf("屬性%s已經(jīng)存在,按任意鍵返回重新輸入",perty_name[i]); getchar(); } elsepropertyname_is_right=1; }//屬性名while //step4:選擇屬性類型 intpropertytype_is_right=0; while(!propertytype_is_right) { system("cls"); printf("表名:%-10s\n",newtable.table_name); printf("屬性個數(shù):%d\n",perty_num); printf("\n"); printf("屬性名屬性類型是否允許為空是否為主鍵\n");printf("(1=int2=float)(1=是2=否)(1=是2=否)\n");printf("\n"); for(j=0;j<i;j++) {printf("屬性%2d:%-10s%d%d%d\n",j+1, perty_name[j],perty_type[j],perty_null[j],perty_key[j]); } printf("屬性%2d:%-10s\n",i+1,perty_name[i]); scanf("%d",&perty_type[i]); getchar(); //判斷選擇的屬性類型是否正確 if(perty_type[i]!=1&&perty_type[i]!=2) { printf("您選擇的屬性類型有誤,按任意鍵重新選擇\n"); getchar(); } elsepropertytype_is_right=1; }//屬性類型while //step5:選擇是否允許為空 intpropertynull_is_right=0; while(!propertynull_is_right) { system("cls"); printf("表名:%-10s\n",newtable.table_name); printf("屬性個數(shù):%d\n",perty_num); printf("\n"); printf("屬性名屬性類型是否允許為空是否為主鍵\n");printf("(1=int2=float)(1=是2=否)(1=是2=否)\n");printf("\n"); for(j=0;j<i;j++) { printf("屬性%2d:%-10s%d%d%d\n",j+1, perty_name[j],perty_type[j],perty_null[j],perty_key[j]); } printf("屬性%2d:%-10s%d\n",i+1,perty_name[i],perty_type[i]); scanf("%d",&perty_null[i]); getchar(); //判斷選擇是否正確 if(perty_null[i]!=1&&perty_null[i]!=2) { printf("您的選擇有誤,按任意鍵重新選擇\n"); getchar(); } elsepropertynull_is_right=1; }//是否允許為空while //step6:選擇是否為主鍵 intpropertypropertykey_is_right=0; while(!propertypropertykey_is_right) { system("cls"); printf("表名:%-10s\n",newtable.table_name); printf("屬性個數(shù):%d\n",perty_num); printf("\n"); printf("屬性名屬性類型是否允許為空是否為主鍵\n");printf("(1=int2=float)(1=是2=否)(1=是2=否)\n");printf("\n"); for(j=0;j<i;j++) { printf("屬性%2d:%-10s%d%d%d\n",j+1, perty_name[j],perty_type[j],perty_null[j],perty_key[j]); } printf("屬性%2d:%-10s%d%d\n",i+1, perty_name[i],perty_type[i],perty_null[i]); scanf("%d",&perty_key[i]); getchar(); //判斷選擇是否正確 if(perty_key[i]!=1&&perty_key[i]!=2) { printf("您的選擇有誤,按任意鍵重新選擇\n"); getchar(); } elsepropertypropertykey_is_right=1; //主鍵的非空屬性應該為不允許空 if(perty_key[i]==1&&perty_null[i]==1) { printf("該屬性設為主鍵,因此改為不允許為空,按任意鍵繼續(xù)\n"); getchar(); perty_null[i]=2; system("cls"); printf("表名:%-10s\n",newtable.table_name); printf("屬性個數(shù):%d\n",perty_num); printf("\n"); printf("屬性名屬性類型是否允許為空是否為主鍵\n");printf("(1=int2=float)(1=是2=否)(1=是2=否)\n"); printf("\n"); for(j=0;j<=i;j++) { printf("屬性%2d:%-10s%d%d%d",j+1, perty_name[j],perty_type[j],perty_null[j],perty_key[j]); } if(i==perty_num-1)printf("\n"); } }//是否為主鍵while }//屬性for(5)修改表boolalter(){chartmp[N];chartable_name[N];boolerror=false;chartype[6][10]={"char","int","float","double","time","date"};stringstreamss(sql);ss>>tmp;if(0!=strcmp(tmp,"alter")){error=true;printf("\nTheword'alter'maybeerror!\n");}else{ss>>tmp;if(0!=strcmp(tmp,"table")){error=true;printf("\nTheword'table'maybeerror!\n");}else{ss>>table_name;strcat(table_name,".txt");if(access(table_name,0)==-1){printf("Thetabledonotexist!!\n");returnfalse;}ss>>tmp;if(strcmp(tmp,"add")==0){ss>>tmp;Fieldfield;strcpy(,tmp);ss>>tmp;boolflg=false;for(inti=0;i<6;++i){if(strcmp(tmp,type[i])==0){flg=true;strcpy(field.type,tmp);break;}}if(!flg){printf("\nThetypemaybewrong(char,int,float,double,time,date)!\n");error=true;returnfalse;}if(field.type[0]=='c'||field.type[0]=='f'||field.type[0]=='d'){ss>>tmp;boolflg=true;intlen=strlen(tmp);intsum=0;for(inti=0;i<len;++i){if(tmp[i]>58||tmp[i]<48){flg=false;break;}elsesum=sum*10+tmp[i]-'0';}if(!flg){error=true;printf("\nThelengthofthetypemaybeerror!!\n");returnfalse;}elsefield.len=sum;}ss>>tmp;if(0==strcmp(tmp,"primary")){charst[N];ss>>st;if(0==strcmp(st,"key")){strcat(tmp,"key");strcpy(field.condition,tmp);}else{error=true;printf("Theword\'key\'maybeerror!!\n");returnfalse;}}elseif(strcmp(tmp,"unique")==0){strcpy(field.condition,tmp);}FILE*fp=fopen(table_name,"a");fwrite(&field,sizeof(Field),1,fp);fclose(fp);}elseif(strcmp(tmp,"drop")==0){ss>>tmp;if(0==strcmp(tmp,"primary")){charst[N];ss>>st;if(0!=strcmp(st,"key")){error=true;printf("Theword\'key\'maybeerror!!\n");returnfalse;}elsestrcat(tmp,"key");}Fieldfield[N];intlen=0;FILE*fp=fopen(table_name,"r");while(fread(&field[len],sizeof(Field),1,fp)!=0){++len;}fclose(fp);remove(table_name);printf("%d\n",len);for(inti=0;i<len;++i){if(strcmp(field[i].condition,tmp)==0)field[i].condition[0]=0;}fp=fopen(table_name,"w");fwrite(&field[0],sizeof(Field),1,fp);fclose(fp);for(inti=1;i<len;++i){fp=fopen(table_name,"a");fwrite(&field[i],sizeof(Field),1,fp);fclose(fp);}}elseif(strcmp("alter",tmp)==0){ss>>tmp;ss>>tmp;charst[N];intsum=0;ss>>st;boolflg=false;for(inti=0;i<6;++i){if(strcmp(st,type[i])==0){flg=true;break;}}if(!flg){printf("\nThetypemaybewrong(char,int,float,double,time,date)!\n");error=true;returnfalse;}if(st[0]=='c'||st[0]=='f'||st[0]=='d'){chart[N];ss>>t;boolflg=true;intlen=strlen(t);for(inti=0;i<len;++i){if(t[i]>58||t[i]<48){flg=false;break;}elsesum=sum*10+t[i]-'0';}if(!flg){error=true;printf("\nThelengthofthetypemaybeerror!!\n");returnfalse;}}Fieldfield[N];intlen=0;FILE*fp=fopen(table_name,"r");while(fread(&field[len],sizeof(Field),1,fp)!=0){++len;}fclose(fp);remove(table_name);for(inti=0;i<len;++i){if(strcmp(field[i].name,tmp)==0){strcpy(field[i].type,st);field[i].len=sum;break;}}fp=fopen(table_name,"w");fwrite(&field[0],sizeof(Field),1,fp);fclose(fp);for(inti=1;i<len;++i){fp=fopen(table_name,"a");fwrite(&field[i],sizeof(Field),1,fp);fclose(fp);}}}}return
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 綠色技術在生態(tài)旅游中的應用前景
- 2025年伺服定位系統(tǒng)合作協(xié)議書
- 2025年在線教育平臺教學質量評估與提升策略:教育質量評價體系研究報告
- 機電工程職場發(fā)展試題及答案
- 機電工程2025年仿真分析試題及答案
- 機電工程標準化管理試題及答案
- 數(shù)字藝術市場2025年創(chuàng)作與交易模式變革研究報告
- 決策支持系統(tǒng)在項目中的應用試題及答案
- 智慧物流配送體系2025年資金申請報告:物流行業(yè)物流機器人市場前景分析
- 西方政治制度與城市治理策略試題及答案
- 安徽省合肥一中2025屆高三最后一卷英語試題及答案
- 2025年法律職業(yè)資格(客觀題)重點考點大全
- 2024年直播電商高質量發(fā)展報告
- 浙江專升本免試題目及答案
- 吉林省長春市2025屆高三質量監(jiān)測(四)英語試卷+答案
- 中等職業(yè)學校英語課程標準
- 北京市海淀區(qū)2023-2024學年五年級下學期語文期末考試試卷(含答案)
- 2025-2030瀝青市場投資前景分析及供需格局研究研究報告
- 剪輯考試試題及答案
- 智能財務導論 課件全套 陳俊 第1-12章 智能財務的發(fā)展 -數(shù)智時代的會計倫理
- 兒童言語康復試題及答案
評論
0/150
提交評論