資源描述:
《數(shù)據(jù)庫建表-建約束-建外鍵.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、usestuCreateTableCourse(CIDVarchar(12)NotNullPrimaryKey,CNameVarchar(20)NotNullUnique,CCridetVarchar(20)NotNull);Select*fromCourse--利用命令為表Course創(chuàng)建主鍵AlterTableCourseAddConstraintPK_CNoPrimaryKey(CID,CName)--刪除約束AlterTableCourseDropConstraintUQ__Course__0425A276--利用語句增加Unique約束AlterTableCou
2、rseAddConstraintUe_CnameUnique(CName)--增加Default約束AlterTableCourseAddConstraintUD_CreditDefault'內(nèi)蒙古呼和浩特'ForCCridet--插入新的記錄InsertIntoCourseValues('001','OOP',Default)Select*fromCourseInsertIntoCourse(CID,CName)Values('002','OOO')select*fromCourse--如果利用Insert語句指定特定的列時(shí),必須包含--不能為空的所有列(不能為空但定義
3、了Default約束--除外)CreateTableStu(SIDVarchar(12)NotNull,SNameVarchar(12)NotNull,SSexChar(2)NotNull)Select*FromStuAlterTableStuAddCOnstraintUD_SexDefault'男'ForSSexInsertIntoStu(SID,SName)Values('001','Herry')Select*FromStu--TimeStamp時(shí)間戳--IdentityCreateTableClass(CIDIntIdentity(1,2),CAddressVar
4、char(20)NotNull)InsertIntoClass(CAddress)Values('惡人都是')Select*fromClassDeleteFromClassWhereCID=33--創(chuàng)建成績表ScoreCreateTableScore(CIDVarchar(12)NotNullPrimaryKey,--成績編號(hào)CNumberVarchar(12)NotNull,--學(xué)號(hào)CNameVarchar(20)NotNull,--課程名稱CScoreDecimal(5,2)NotNull)Select*FromScore--創(chuàng)建CHeck約束AlterTableSc
5、oreAddConstraintCK_BrandCheck(CScorebetween0and100)InsertIntoScoreValues('34','34','34',10.00)select*fromScore--ForeignKey外鍵--創(chuàng)建員工表Employee和工資表SalaryCreateTableEmployee(EIDVarchar(12)NotNullPrimaryKey,--員工號(hào)ENameVarchar(20)NotNull--員工姓名)CreateTableSalary(SIDVarchar(12)NotNullPrimaryKey,--工
6、資編號(hào)EIDVarchar(12)NotNull)AlterTable球隊(duì)名AddConstraintFK_球隊(duì)名ForeignKey(球隊(duì)名)References球隊(duì)表(球隊(duì)名)