创建与维护MySQL数据包表

1: 登录MySQL

输入 show databases; 查看一下所有数据库

随后输入 use 表名 ,使用数据库

出现 Database changed 就表示成功了

之后输入create table  表名()括号中输入你的字段与数据类型,

可以使用comment"" 在数据类型后增加注释信息

 出现Query OK, 0 rows affected (0.03 sec)  就代表成功了

我们查看一下表的创建信息

 之后我们可以尝试修改表名

我们将数据表修改

 我们查看一下数据表信息

 可以看到表名已经被修改了

随后我们可以修改字段名 和 数据类型

输入 alter table 表名 change 旧name 新name 字段char(5);   就可以修改表的字段 信息

 添加字段

输入 alter table 表名 add (新字段)即可添加新字段

例如:

 输入 alter table 表名 add (新字段)新数据类型 后加 after 便可 将此字段添加到任意字段后

例如:

 修改字段位置

输入

alter table 表名 modify 待修改字段名 数据类型 after 字段名:

例如:

 删除字段
alter table 表名drop 字段名;

例如:

 我们查看一下数据表

 最后删除数据表 输入drop table 表名;

退出MySQL

 

 非空 not null
唯一unique
默认 default"默认值"自增auto increment

单字段主键
create table 表名(
字段名1数据类型1COMMENT"备注信息1" primary key,字段名2数据类型2COMMENT"备注信息2");
多字段主键
create table 表名(
字段名1数据类型1COMMENT"备注信息1",字段名2 数据类型2COMMENT"备注信息2",字段名3数据类型3COMMENT"备注信息3", primary key(字段名1,字段名2));
create table classroom(
build varchar(5)comment "楼栋", classroom no int comment"教室号", seat int comment "座位数"
primary key(build,classroom no));

外键
constraint 字段别名一般为fk本表字段名foreign key(本表字段名)references 来源表名(来源表中字段名)
班级表中 主键:班级名 其他字段:人数、班长 create table class(
name varchar(20) primary key, count int,
monitor varchar(5)comment"班长");
教师表 主键:教师号 其他字段:教师名 create table teacher(
no varchar(8) comment "教师号" primary key, name varchar(20));
课表 外键:班级名、教师号 其他字段:课程名、.... create table timetable(
class name varchar(20), teacher no varchar(8),
course varchar(20) comment"课程名"
constraint fk class name foreign key (class name) references class(name) constraint fk teacher no foreign key (teacher no) references teacher(no)
);
我:
非空 not null唯一unique
默认 default"默认值"自增auto increment
create table book(
id int comment "编号" primary key auto_increment, name varchar(20)comment "书名" not null, isbn varchar(30) not null unique,
is_color enum("1","2")comment "是否彩印 1是 2否"default "2");

创建教师表

输入 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"计算机系"
    -> );

查看

 

 创建课程表

 create table course(
    -> no char(4) comment"课程号" primary key,
    -> name varchar(20) comment"课程名" not null unique,
    -> class_hours int(2) comment"课时数" default"45"
    -> );
Query OK, 0 rows affected, 1 warning (0.03 sec)

 

查看

 

 创建授课表

输入

 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)
    -> );

查看

 

 创建成绩表

输入 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)
    -> );

 

 这就是MySQL数据表的一些基础的创建与维护了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值