0
点赞
收藏
分享

微信扫一扫

Mysql——表的约束

yongxinz 2022-03-13 阅读 71
mysqllinqp2p

打开管理员终端

登入Mysql,输入:

Mysql  -uroot -p******

创建数据库:

create database 数据库名;

 

使用数据库

use 数据库名;

 

创建数据表teacher,输入:

create table teacher(
    -> no char(4) comment"教师号" primary key,
    -> name varchar(10) comment"教师姓名" not null,
    -> prof varchar(20) comment"职称" not null default"助教",
    -> sal int(2) comment"工资"not null,
    -> comm smallint(2) comment"岗位津贴"
    -> );


创建数据表student,输入: 

 create table student(
    -> no char(4) comment"学生号" primary key,
    -> name varchar(10) comment"学生姓名" not null,
    -> age tinyint(1) comment"年龄" not null,
    -> dept varchar(20) comment"系名" not null default"计算机系"
    -> );

创建数据表course,输入:

 create table course(
    -> no char(4) comment"课程号" primary key,
    -> name varchar(20) comment"课程名" not null unique,
    -> class_hours int(2) comment"课时数" default"45"
    -> );

创建数据表 school_teaching,输入:

create table school_teaching(
    -> id int(4) comment"序号" primary key auto_increment,
    -> course_no char(4) comment"课程号" not null,
    -> teacher_no char(4) comment"教师号" not null,
    -> week int(2) comment "周数" default"15",
    -> class_num varchar(10) comment"教师号",
    -> constraint fk_course_no foreign key (course_no) references course(no),
    -> constraint fk_teacher_no foreign key (teacher_no) references teacher(no)
    -> );

创建数据表chengweiqiang_grade,输入:

 create table chengweiqiang_grade(
    -> course_no char(4) comment"课程号" not null,
    -> student_no char(4) comment"学生号" not null,
    -> score float comment"成绩" not null default"60",
    -> primary key(course_no,student_no)
    -> );


删除数据表,输入: 

drop table 表名;

删除数据库,输入:

drop datablase 数据库名;

 

退出Mysql,输入:

\q;

举报

相关推荐

【MySQL】表的约束

MYSQL表的约束

Mysql表的约束

MySQL 表的约束

【mysql】—— 表的约束

MySQL表的约束

【MySQL--->表的约束】

0 条评论