第二章 数据库管理操作 ② 代码
1.查看表格的方式
表格最原始的两种查看方式:表单方式查看和网格方式查看。关闭的话就直接将打开的表格文件关闭了。
1.1表单方式查看
1.2网格方式进行查看
1.3查看表格文件的设计表
1.查看挡位
2.查看索引
3.查看外键
4.查看触发器
5.查看选项
6.关闭
点击关闭,将会返回到表格的表单/网格方式页面(取决于查看设计表之前,表格的查看状态是表单方式查看还是表格方式查看。这里是表格方式页面)。
2.实体完整性(Entity Integrity)约束
#实体完整性: 要保证数据的唯一性
#1.主键约束 primary key : 加了主键约束的列,唯一且不能为空。
#2.自增约束 auto_increment: 加了自增约束的列,列值会自动按序生成。
# a.必须在主键列上加自增约束
# b.自增列必须是整数
# c.一个表只能有一个自增
# d.自增列不一定都是连续的
#3.unique约束(唯一约束): 加了唯一约束的列,列值必须唯一,但是列的值可以为空
# 唯一约束+非空约束是否可以替代主键? 不可以,主键会生成主键索引
#删除表
drop table student;
#创建学生表
create table student
(
id int primary key auto_increment, #主键约束,自增约束
name varchar(20),
sex char(1),
birthday date,
phone varchar(11) UNIQUE #唯一约束
);
#查询表所有信息/所有项/所有数据
select * from student;
#查看表结构
desc student;
show create TABLE STUDENT;
-- CREATE TABLE `student` (
-- `id` int NOT NULL AUTO_INCREMENT,
-- `name` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL,
-- `sex` char(1) COLLATE utf8mb4_general_ci DEFAULT NULL,
-- `birthday` date DEFAULT NULL,
-- `phone` varchar(11) COLLATE utf8mb4_general_ci DEFAULT NULL,
-- PRIMARY KEY (`id`),
-- UNIQUE KEY `phone` (`phone`)
-- ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
--
#查看表元素
SELECT * FROM STUDENT;
#自增列: 省略主键值时,会自动生成
insert into student
(name,sex,birthday)
VALUES
('张三','男','2000-01-01');
#自己指定自增列的值
insert into student
(id,name,sex,birthday)
VALUES
(10,'张三','男','2000-01-01');
#插入重复主键
insert into student
(id,name,sex,birthday)
VALUES
(10,'张三','男','2000-01-01'); #主键去重
#查询数据
select * from student;
#=============================================
#唯一约束unique:对应列的值必须唯一
insert into student
(name,sex,birthday,phone)
VALUES
('张三','男','2000-01-01','13112345678');
insert into student
(name,sex,birthday,phone)
VALUES
('张三','男','2000-01-01',null);
#查询数据
select * from student;
#删除表
drop table student;
分析代码:
新建数据表设计表/表结构(和下图类似):
查看表结构:
查看实际的建表语句(老师这么描述):
查询数据:
2.1主键约束 primary key
2.2自增约束 auto_increment
2.3唯一约束unique
3.域完整性(Domain Integrity)
#域完整性(domain):针对数据范围进行限定
#非空约束 not null: 加了非空约束,则该列值不能为空
#默认值约束 default : 加了默认值约束的列,如果没有提供值,则用默认值填充。
# 如果提供了值,则使用提供的值填充列
#外键约束 foreign key : 加了外键约束的列,列的值要跟关联表一致
#删除表
drop table student;
#创建学生表
create table student
(
id int primary key auto_increment, #主键约束,自增约束
name varchar(20) not null, #非空
sex char(1) not null, #非空
birthday date, #允许空
phone varchar(11) UNIQUE, #唯一约束
address varchar(20) default '河南' #默认值约束
);
#查询数据
select * from student;
#查看表结构
desc student;
show create TABLE STUDENT;
-- CREATE TABLE `student` (
-- `id` int NOT NULL AUTO_INCREMENT,
-- `name` varchar(20) COLLATE utf8mb4_general_ci NOT NULL,
-- `sex` char(1) COLLATE utf8mb4_general_ci NOT NULL,
-- `birthday` date DEFAULT NULL,
-- `phone` varchar(11) COLLATE utf8mb4_general_ci DEFAULT NULL,
-- `address` varchar(20) COLLATE utf8mb4_general_ci DEFAULT '河南',
-- PRIMARY KEY (`id`),
-- UNIQUE KEY `phone` (`phone`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
#非空约束:列值不能为空,必须提供
insert into student
(name,sex,birthday)
VALUES
('aaa','a','2000-01-01');
insert into student
(birthday,phone)
VALUES
('2000-01-01','12312312');
#查询数据
select * from student;
#默认值约束:
/*
加了默认值约束的列,如果没有提供值,则用默认值填充。
如果提供了值,则使用提供的值填充列
*/
insert into student
(name,sex,birthday)
VALUES
('aaa','a','2000-01-01');
insert into student
(name,sex,birthday,address)
VALUES
('aaa','a','2000-01-01',null);
insert into student
(name,sex,birthday,address)
VALUES
('aaa','a','2000-01-01','河北');
#查询数据
select * from student;
#删除表
drop table student;
3.1非空约束
3.2默认值约束
3.3.外键约束 foreign key
#域完整性(domain):针对数据范围进行限定
#非空约束 not null: 加了非空约束,则该列值不能为空
#默认值约束 default : 加了默认值约束的列,如果没有提供值,则用默认值填充。
# 如果提供了值,则使用提供的值填充列
#外键约束 foreign key : 加了外键约束的列,列的值要跟关联表一致
#外键约束测试
create table classInfo
(
classId int primary key auto_increment, #注意auto_increment中的下划线_!!!!!
className varchar(20) not null
);
create table student
(
sid int primary key auto_increment,
name varchar(20),
sex char(20),
clsId int, #所属班级
foreign key (clsId) #指定当前表那个列做外键 #注意这里不加逗号!!!!!!!
references classInfo(classId) #指定要关联另外一个表的哪个字段
);
select * from classInfo;
select * from student;
delete from classInfo where classId = 1;
update classInfo
set classId = 10
where classId = 1;
insert into classInfo
(classname)
select 'AAA01' UNION
select 'AAA02' UNION
select 'AAA03' ;
insert into student
(name,sex,clsId)
select '张三','男',1 UNION
select '李四','女',3 ;
select * from classInfo;
select * from student;
insert into student
(name,sex,clsId)
select '张三','男',2 ;
#删除表
drop table student;
drop table classInfo;
4.课前测试
#创建数据库
create database 70715_db1 #今日份命名
default character set utf8mb4 #设置字符集
default collate utf8mb4_general_ci #设置排列规则
#创建表
create table category
(
categoryId int primary key,
categoryName char(20) not null
);
create table good
(
goodld int primary key auto_increment,
goodName char(20) not null,
price decimal(5,1), #小数位数!!!
produceDate date not null,
address char(20) default '郑州',
categoryId int,
foreign key(categoryId) references category(categoryId)
);
#插入数据
insert into category
(categoryId,categoryName)
values
(1,'洗漱用品'), #主键约束 非空 自增的话可以不写!!!
(2,'使用品'),
(3,'食用品');
insert into good
(goodName,price,produceDate,categoryId)
values
('牙刷1',3.2,'2020-01-01',1),
('牙刷2',3.1,'2020-01-01',1),
('牙刷3',3.3,'2020-01-01',1),
('水杯',3.4,'2020-01-01',2),
('方便面',3.5,'2020-01-01',3);
#查看数据
select * from category;
select * from good;
5.总结:
约束,数据完整性
1.实体完整性:数据的唯一性
a.主键约束 primary key : 唯一,非空,会生成主键索引。
一个表只能有一个主键
一个主键可以由多个列组成
b.自增约束 auto_increment :整数列,数据自动增长,依赖于主键
一个表中只能有一个自增列
c.unique唯一约束 : 唯一,允许空
2.域完整性: 数据范围
a.非空约束 not null : 列值不能为空
b.默认值约束 default : 不提供值的时认值填充候用默
c.外键约束 foreign key...references.. : 从表外键列依赖于主表主键
一个表可以有多个外键
3.引用完整性
a.外键约束 foreign key...references..
1).从表中外键列插入的值必须在主表中存在
2).主表中被从表引用的数据,不能删除
3).主表中被从表引用的数据,不能修改
4.用户自定义完整性
6.数据定义语言DDL
动态添加/修改/删除-列/列数据类型/列名/列主键约束等。这样的话,数据类型建表错误的时候,就不用删表重建啦~
主要用来修改数据表结构用的,增加/减少/修改 字段 (属性) 添加各种约束
#创建表
create table classInfo
(
classId int primary key auto_increment,
className varchar(20) not null
)
select * from classInfo;
create table student
(
id int,
name varchar(20)
)
#动态添加列
#ddl
#添加列
alter table student
add sex char(1)
#删除列
alter table student
drop column sex
#修改列的类型
alter table student
modify name varchar(200)
#修改列名
alter table student
change name stuname varchar(100)
#添加主键约束
alter table student
modify id int primary key
#删除主键
alter table student
drop primary key
#添加列和默认值
alter table student
add address varchar(20) default '河南'
-- #动态添加列
-- alter table student
-- add address2 char(20);
--
-- select * from student;
--
-- #动态添加默认值约束
-- alter table student
-- modify address2 char(20) default '河南';
#修改数据类型和默认值约束
alter table student
modify address varchar(200) default '北京'
#添加外键约束
#1.加列
alter table student
add clsId int
#2.加外键约束
alter table student
add CONSTRAINT fk_student_cls_clsId #添加约束
FOREIGN key (clsId)
REFERENCES classInfo(classId)
#3.删除外键约束
alter table student
drop FOREIGN key fk_student_cls_clsId
select * from student;
desc student;
创建表:
(下图为右键单击student表,选择查看设计表的页面)
1.动态插入列:
2.动态删除列:
3.动态修改列:
4.动态改变类名和数据类型:
5.动态添加主键约束
6.动态删除主键
7.添加列和默认值约束
(下图为右键单击student表,选择查看设计表的页面)
等同于:
8.动态删除列
9.修改数据类型和默认值
10.添加外键约束
#添加外键约束
alter table 表名
add constraint 约束名 #此处约束名自己起,不起系统会自动帮起名
foreign key(当前表列名)
references 关联表(关联表列名)
#删除外键约束
alter table 表名
drop foreign key 外键约束名
11.删除外键约束
代码:(逐句运行)
#创建表
create table classInfo #班级表
(
classId int primary key auto_increment,
className varchar(20) not null
);
select * from classInfo;
create table student
(
id int,
name varchar(20)
);
select * from student;
desc student;
#动态添加列
alter table student
add sex char(1);
select * from student;
desc student;
#动态删除列
alter table student
drop column sex; #column 列
select * from student;
desc student;
#动态修改类的类别
alter table student
modify name char(200);#modify 修改
select * from student;
desc student;
#动态修改列名和数据类型
alter table student
change name stuname varchar(100);
select * from student;
desc student;
#动态添加主动约束
alter table student
modify id int primary key;
select * from student;
desc student;
#动态删除主键
alter table student
drop primary key; #一个表只允许有一个主键!!!
select * from student;
desc student;
#动态添加列和默认值约束
alter table student
add address char(20) default '河南';
select * from student;
desc student;
#动态添加列
alter table student
add address2 char(20);
#动态添加默认值约束
alter table student
modify address2 char(20) default '河南';
select * from student;
desc student;
#注意:动态添加的列 只能动态删除!!!
#第一种方式:动态删除列
alter table student
drop address2;
#第二种方式:动态删除列
alter table student
drop column address2;
select * from student;
desc student;
#修改数据类型和默认值
alter table student
modify address char(100) default '北京';
select * from student;
desc student;
-- #添加外键约束
-- alter table 表名
-- add constraint 约束名
-- foreign key(当前表列名)
-- references 关联表(关联表列名)
--
-- #删除外键约束
-- alter table 表名
-- drop foreign key 外键约束名
--
#添加外键约束
#1.加列
alter table student
add clsId int;
#加外键约束
alter table student
add constraint fk_student_cls_clsId #添加约束
foreign key(clsId)
references classInfo(classId);
select * from student;
desc student;
#删除外键约束
alter table student
drop foreign key fk_student_cls_clsId;
select * from student;
desc student;
课后作业2
课后作业3
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118113.html