MySQL基础(创建库,创建表,添加数据)

时间:2022-04-03 12:07:38
CREATE DATABASE 数据库名;

CREATE TABLE student2(
sno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"学号",
sname VARCHAR(20) NOT NULL COMMENT"学生姓名",
ssex VARCHAR(20) NOT NULL COMMENT"学生性别",
sbirthday datetime COMMENT"学生出生年月",
class VARCHAR(20) COMMENT"学生所在班级"
);

CREATE TABLE Score2(
Sno VARCHAR(20) NOT NULL COMMENT"学号(外码)",
Cno VARCHAR(20) NOT NULL COMMENT"课程号(外码)",
Degree DECIMAL(4,1) COMMENT"成绩",
constraint sno FOREIGN KEY(sno) REFERENCES student2(sno),
constraint Cno FOREIGN KEY(Cno) REFERENCES Course(Cno)
);

CREATE TABLE Teacher(
Tno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"教工编号(主码)",
Tname VARCHAR(20) NOT NULL COMMENT"教工姓名",
Tsex VARCHAR(20) NOT NULL COMMENT"教工性别",
Tbirthday datetime COMMENT"教工出生年月",
Prof VARCHAR(20) COMMENT"职称",
Depart VARCHAR(50) NOT NULL COMMENT"教工所在部门",
constraint Tno unique(Tno)
);

#constraint Tno FOREIGN KEY(Tno) REFERENCES test.Teacher(Tno)/test.是库的意思
CREATE TABLE Course(
Cno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"课程号(主码)",
Cname VARCHAR(20) NOT NULL COMMENT"课程名称",
Tno VARCHAR(20) NOT NULL COMMENT"教工编号(外码)",
constraint Tno FOREIGN KEY(Tno) REFERENCES Teacher(Tno)
);

#以上四个表的运行顺序是1342

#以下是表的数据

#Student表数据
INSERT INTO student2 values(108,"曾华","男",19770901,95033);
INSERT INTO student2 values(105,"匡明","男",19751002,95031);
INSERT INTO student2 values(107,"王丽","女",19760123,95033);
INSERT INTO student2 values(101,"李军","男",19760220,95033);
INSERT INTO student2 values(109,"王芳","女",19750210,95031);
INSERT INTO student2 values(103,"陆君","男",19740603,95031);
#Course表数据
INSERT INTO course values ("3-105","计算机导论",825);
INSERT INTO course values ("3-245","操作系统",804);
INSERT INTO course values ("6-166","数字电路",856);
INSERT INTO course values ("9-888","高等数学",831);
#Score表数据
INSERT INTO score2 values(103,"3-245",86 );
INSERT INTO score2 values(105,"3-245",75 );
INSERT INTO score2 values(109,"3-245",68 );
INSERT INTO score2 values(103,"3-105",92 );
INSERT INTO score2 values(105,"3-105",88 );
INSERT INTO score2 values(109,"3-105",76 );
INSERT INTO score2 values(101,"3-105",64 );
INSERT INTO score2 values(107,"3-105",91 );
INSERT INTO score2 values(108,"3-105",78 );
INSERT INTO score2 values(101,"6-166",85 );
INSERT INTO score2 values(107,"6-166",79 );
INSERT INTO score2 values(108,"6-166",81 );

#Teacher表数据
INSERT INTO teacher values(804,"李诚","男",19581202,"副教授","计算机系");
INSERT INTO teacher values(856,"张旭","男",19690312,"讲师","电子工程系");
INSERT INTO teacher values(825,"王萍","女",19720505,"助教","计算机系");
INSERT INTO teacher values(831,"刘冰","女",19770814,"助教","电子工程系");

#以上四个表的数据运行顺序是1423