Java学习笔记——MySQL创建表结构
一.创建/删除数据库.
1 create database t14; 2 drop database t14; 3 use t14;
二.创建若干表用于测试
这里预留了几个坑,下面要填坑的..
1 /*创建学生表*/ 2 create table student( 3 studentNo int(4) PRIMARY KEY not null, 4 loginPwd VARCHAR(20) not null, 5 studentName VARCHAR(50) not NULL, 6 sex char(2) not null DEFAULT '男', 7 gradeID int(4) UNSIGNED, 8 phone VARCHAR(50), 9 address VARCHAR(255) default '地址不详', 10 bornDate DATETIME, 11 email VARCHAR(50), 12 identifyCard VARCHAR(18) UNIQUE 13 ) 14 /*创建年级表*/ 15 drop table if exists grade 16 create table grade( 17 gradeID int primary key not null auto_increment, 18 gradeName VARCHAR(32) not null 19 ) 20 /*创建科目表*/ 21 CREATE TABLE `subject` ( 22 `subjectNo` int(4) NOT NULL auto_increment, 23 `subjectName` varchar(50), 24 `classHour` int(4), 25 `gradeID` int(4), 26 PRIMARY KEY (`subjectNo`) 27 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 28 /*创建成绩表*/ 29 drop table if exists `result` 30 CREATE TABLE `result` ( 31 `resultNo` int not null auto_increment, 32 `studentNo` int(4) not null, 33 `subjectNo` int(4) NOT NULL, 34 `examDate` DATETIME not NULL DEFAULT NOW(), 35 `studentResult` int(4) not NULL, 36 PRIMARY KEY (`resultNo`) 37 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
添加汉字数据的时候如果报错,说什么未定义的数据类型,就把你的默认字符集latin改成utf8就行了.具体方法不赘述了
三.添加外键约束
1 /*给student,grade表创建外键约束*/ 2 alter table `student` add CONSTRAINT fk_grade_student_gradeID foreign KEY(`gradeID`) REFERENCES `grade` (`gradeID`)
运行这行代码会报错.
SQL Error [1215] [HY000]: Cannot add foreign key constraint
java.sql.SQLException: Cannot add foreign key constraint
下面总结几个创建外键失败的原因:
1.企图在主表创建外键
2.两表中有非法记录
3.两表公共列数据类型不同(长度,特性eg.unsigned)
这里grade表中gradeID的数据类型及特性是int ,大家都知道int的默认长度是11
student表中gradeID的数据类型是 int(4) UNSIGNED
两字段数据长度及特性不一致.
1 alter table student change gradeID gradeID int 2 desc student 3 alter table `student` add CONSTRAINT fk_grade_student_gradeID foreign KEY(`gradeID`) REFERENCES `grade` (`gradeID`)
修改Student表中gradeID数据类型为int,再执行添加外键的操作,添加外键成功.
然后添加其他外键.
1 /*subject,grade表创建外键约束*/ 2 alter table subject change gradeID gradeID int 3 desc subject 4 alter table `subject` add CONSTRAINT fk_grade_subject_gradeID foreign KEY(`gradeID`) REFERENCES `grade` (`gradeID`) 5 /*学生表与成绩表创建外建约束*/ 6 alter table `result` add CONSTRAINT fk_student_result_studentNo foreign KEY(`studentNo`) REFERENCES `student` (`studentNo`) 7 /*科目表与成绩表创建外建约束*/ 8 alter table `result` add CONSTRAINT fk_subject_result_subjectNo foreign KEY(`subjectNo`) REFERENCES `subject` (`subjectNo`)
到这里就完成了四个表的外键约束.
4.为student表中studentNo添加自增
当初创建表的时候没加自增,现在想加了,怎么办?
1 alter table student change studentNo studentNo int(4) NOT NULL auto_increment
使用alter语句发现报错.
SQL Error [1832] [HY000]: Cannot change column 'studentNo': used in a foreign key constraint 'fk_student_result_studentNo'
java.sql.SQLException: Cannot change column 'studentNo': used in a foreign key constraint 'fk_student_result_studentNo'
没办法了,这里只能先删除外键约束,再添加自增.
1 /*删除外键约束*/ 2 ALTER TABLE `result` DROP FOREIGN KEY fk_student_result_studentNo 3 ALTER TABLE `result` DROP FOREIGN KEY fk_subject_result_subjectNo 4 ALTER TABLE `subject` DROP FOREIGN KEY fk_grade_subject_gradeID 5 ALTER TABLE `student` DROP FOREIGN KEY fk_grade_student_gradeID
一共四条外建约束,需要删除哪条约束就执行哪行吧.
删除外键之后四个表就很干净了,没有任何关联.然后可以进随便行修改字段,truncate table 等操作.
1 /*想要truncate table 必须先清除外建约束 2 * truncate 语句不记录日志,删除后自增列从1开始,只能删除整个表数据 3 * delete 记录日志,删除后自增列序号断裂,+where条件可删除若干行 4 * truncate删除得更彻底,性能比delete高 5 * */ 6 TRUNCATE TABLE subject 7 TRUNCATE TABLE student 8 TRUNCATE TABLE `result` 9 TRUNCATE TABLE grade
执行添加自增操作:
1 alter table student change studentNo studentNo int(4) NOT NULL auto_increment
下面可以添加测试数据了.这里因为有外键约束,所以注意一下先添加主表数据,再添加从表数据.然后不要有非法数据就可以了.
1 /*为各表插入数据*/ 2 insert into grade (gradeName) VALUES('T15') 3 insert into subject (subjectName,classHour,gradeID) VALUES('语文',60,1),('数学',60,1) 4 desc student 5 insert into student(loginPwd,studentName,sex,gradeID,phone,address,bornDate,email,identifyCard) VALUES 6 ('000000','强哥','女',1,'120','新加坡',now(),'qq@126.com','410523195601016754') 7 desc `result` 8 insert into `result`(studentNo,subjectNo,examDate,studentResult) VALUES(1,1,now(),90) 9 insert into `result`(studentNo,subjectNo,examDate,studentResult) VALUES(1,2,now(),90)
最后附上表图.