网站建设aichengkeji,wordpress的主题博客,店铺引流推广方案,福建建设工程交易网站1、workbench简介#xff1a;
登录客户端的两种方法 在cmd中#xff0c;只能通过sql语句控制数据库#xff1b;workbench其实就是一种图形化数据库管理工具#xff0c;在workbench中既可以通过sql语句控制数据库#xff0c;也可以通过图形化界面控制数据库。通过workbenc…1、workbench简介
登录客户端的两种方法 在cmd中只能通过sql语句控制数据库workbench其实就是一种图形化数据库管理工具在workbench中既可以通过sql语句控制数据库也可以通过图形化界面控制数据库。通过workbench登录的方法 2、数据库语言SQL
结构化语言简介 创建数据表的思路 由很多数据表的集合就构成了数据库数据库操作指令 注意没事干别删除系统默认的库sys因为这个里面是mysql的一些配置文件。关于sql文件的保存可以保存在任何位置注意起名字的时候加的反引号和单引号不是一种符号 workbench中字体、字号设置注意变更过字体字号之后要关掉编辑的sql文档重新打开才会生效 3、数据类型★★★★★定义数据库中数据表的时候必须要用所以很重要
在cmd登录mysql平台后查看数据类型帮助文档的指令‘? data types; ’对于显示的数据类型如果由不太懂的也可以用指令查看帮助文档‘? 数据类型名’ 注意一个汉字大概有3个字符 4、约束6种主键约束自增长约束、唯一约束、非空约束、默认值约束、外键约束、注解约束 前言创建数据表的SQL语句如下在创建时就需要添加约束 注意约束是可以累加的也就是说一个字段可以有多个约束 主键约束 注意还有一种自增长约束auto_increment是和主键约束一起来用的当主键是整数类型的时候我们可以使用自增长来避开数据重复问题。SQL写法字段名 int primary key auto_increment 唯一约束 非空约束 默认值约束 外键约束 constraint 【n.限制束缚】 注意constraint 约束名 foreign key字段名3reference 主表名主键字段名中约束名就是主表和分表之间外键约束的名字一般叫做“主表名_分表名”。其实也可以将这个约束理解为主表和分表的联系。 注解约束
练习
练习要求为某个班级建立一套数据库表格包含四个表分别是学生、选修课程、教师、选课记录练习实践 0、先创建新的数据库然后切换到该数据库1、确立各个数据表之间的关系一对一、一对多、多对多2、确定每个数据表的属性3、确定表中每个属性的约束 sql代码
-- 创建一个新的数据库并且选择新的数据库
-- create database if not exists db_exercise charsetutf8mb4;
-- show databases;
-- alter database db_exercise charsetutf8mb4;
-- 切换到新建数据库
use db_exercise;/*
学生学号【主键】 姓名 出生日期 性别【默认】 籍贯
教师编号【主键】 姓名 职称
选修课程编号【主键】 名称 学分 授课教师编号
选课记录记录编号【主键】 学生学号【外键】 课程编号【外键】 选课日期 分数保证学生编号与课程编号联合起来的唯一性unique(cc_sid,cc_cid)表示cc_sid字段和cc_cid字段联系起来是唯一的
*/
create table if not exists tb_student(
stu_id int primary key auto_increment comment 学生学号,
stu_name char(15) comment 姓名,
stu_brid date comment 出生日期,
stu_gender char(3) default 男 comment 学生性别,
stu_location char(10) comment 学生籍贯
);
-- 注意表格名前加‘tb_’是为了和后面要学习的视图做区别create table if not exists tb_teacher(
tea_id int primary key comment 教师编号,
tea_name char(15) comment 教师姓名,
tea_post char(15) comment 教师职称
);create table if not exists tb_c_class(
cl_id int primary key comment 选修课编号,
cl_name char(30) comment 选修课名称,
cl_credit int comment 学分,
cl_tea_id int comment 授课教师,
constraint tea_cl foreign key(cl_tea_id) references tb_teacher(tea_id)
);create table if not exists tb_cc_log(
cc_id int comment 记录编号,
cc_sid int comment 选课记录中的学生编号,
cc_cid int comment 选课记录中的课程编号,
cc_date date comment 选课日期,
cc_score decimal(4,1) comment 分数,
primary key(cc_id),
constraint fk_cc_stu foreign key(cc_sid) references tb_student(stu_id),
constraint fk_cc_cl foreign key(cc_cid) references tb_c_class(cl_id),
constraint uk_stu_cl unique(cc_sid,cc_cid)
);-- 查看表结构语句
desc tb_student5、数据表操作
修改数据表结构 6、数据操作语言DML
在数据表结构已有的情况下向表中添加行数据修改行数据删除行数据。添加 练习给上一个练习中的表格填充数据
注意在执行SQL语言的过程中不要重复执行创建同一个表格或者数据库的指令会出现警告。1、首先通过数据表删除语句将建立的数据表删除然后再次建立数据表。2、给数据表填充数据并查看数据表。提示数据查询语言DQL简单查询通过该语句就可以看到数据表当中的数据是否插入成功 sql代码
-- 切换到新建数据库
use db_exercise;-- 删除表操作
drop table if exists tb_c_class;
drop table if exists tb_cc_log;
drop table if exists tb_student;
drop table if exists tb_teacher;/*
学生学号【主键】 姓名 出生日期 性别【默认】 籍贯
*/
create table if not exists tb_student(
stu_id int primary key auto_increment comment 学生学号,
stu_name char(15) comment 姓名,
stu_brid date comment 出生日期,
stu_gender char(3) default 男 comment 学生性别,
stu_location char(10) comment 学生籍贯
);
-- 注意表格名前加‘tb_’是为了和后面要学习的视图做区别
/*
教师编号【主键】 姓名 职称
*/
create table if not exists tb_teacher(
tea_id int primary key comment 教师编号,
tea_name char(15) comment 教师姓名,
tea_post char(15) comment 教师职称
);
/*
选修课程编号【主键】 名称 学分 授课教师编号
*/
create table if not exists tb_c_class(
cl_id int primary key comment 选修课编号,
cl_name char(30) comment 选修课名称,
cl_credit int comment 学分,
cl_tea_id int comment 授课教师,
constraint tea_cl foreign key(cl_tea_id) references tb_teacher(tea_id)
);
/*
选课记录记录编号【主键】 学生学号【外键】 课程编号【外键】 选课日期 分数保证学生编号与课程编号联合起来的唯一性unique(cc_sid,cl_id)表示stu_id字段和cl_id字段联系起来是唯一的
*/
create table if not exists tb_cc_log(
cc_id int comment 记录编号,
cc_sid int comment 选课记录中的学生编号,
cc_cid int comment 选课记录中的课程编号,
cc_date date comment 选课日期,
cc_score decimal(4,1) comment 分数,
primary key(cc_id),
constraint fk_cc_stu foreign key(cc_sid) references tb_student(stu_id),
constraint fk_cc_cl foreign key(cc_cid) references tb_c_class(cl_id),
constraint uk_stu_cl unique(cc_sid,cc_cid)
);-- 先查看学生表结构
desc tb_student;
-- 学生表添加数据
insert into tb_student values
(1,张三,2000-11-1,default,河北省),
(2,李四,1998-1-1,default,甘肃省),
(3,王五,1997-2-3,女,河南省),
(4,赵六,2003-11-11,default,北京市);
-- 查询下数据是否录入
select * from tb_student;-- 先查看教师表结构
desc tb_teacher;
-- 教师表添加数据
insert into tb_teacher values
(1,刷子,讲师一级),
(2,坦克,讲师一级),
(3,天天,讲师三级),
(4,芒果,讲师三级);
-- 查询下数据是否录入
select * from tb_teacher;-- 先查看课程表结构
desc tb_c_class;
-- 课程表添加数据
insert into tb_c_class values
(1001,生物,3,1),
(1002,地理,2,3),
(1003,化学,3,2),
(1004,历史,1,4);
-- 查询下数据是否录入
select * from tb_c_class;-- 先查看课程记录表结构
desc tb_cc_log;
-- 课程记录表添加数据
insert into tb_cc_log values
(1,1,1001,2021-9-1,64),
(2,2,1002,2021-9-2,74),
(3,3,1003,2021-9-3,84),
(4,4,1004,2021-9-4,94);
-- 查询下数据是否录入
select * from tb_cc_log;