首先说一下,我学习的是MySQL数据库,使用的软件是Navicat Premium12,软件的话就自行百度下载吧,毕竟是商业软件,这里就不提供了,当然,也可以使用SQLyog、MySQL-Front等等其他软件来学习MySQL。
好,进入正题,今天是第一天学习MySQL数据库,首先通过下面的代码创建一张表
create table user (
userId int(11) not null auto_increment comment '用户主键',
userName varchar(30) not null comment '用户名',
userPass varchar(30) not null comment '用户密码',
nikeName varchar(20) default '匿名' comment '昵称',
telephone varchar(20) comment '联系方式',
primary key (userId)
)
运行后即可建成如下所示的表,建成的表只有表头,还未有内容添入
userId | userName | userPass | nikeName | telephone |
null | null | null | 匿名 | null |
在这里说一下varchar这个数据类型和char的区别在哪里:
- char类型的长度是固定的,牺牲了空间来提高性能
- varchar类型的长度是可变的,牺牲性能来换取空间
表结构的修改操作
下面再就是表结构的修改操作,共分为四部分:
1.在原有的基础上增加新的列定义:add
alter table user add (
gender char(1) default '男' comment '性别',
email varchar(30) comment '邮箱'
)
结果为下方表格所示
userId | userName | userPass | nikeName | telephone | gender | |
null | null | null | 匿名 | null | 男 | null |
2.修改列定义:modify
针对已有的列,修改数据类型、默认值、注释等,但是列名不能修改,而且一次只能修改一
列
alter table user modify gender char(2) comment '用户的性别';
3.修改列名:change
不仅可以修改列名,同样可以改当前列的其他项
alter table user change gender gen char(1) default '女' comment '性别';
4.删除列:drop
alter table user drop email;
表的删除操作
下面是表的删除的一些操作:
- 如果需要删除表的话,可以使用下面方法进行删除
drop table user;
- 只删除表中数据,但是保留表结构,可以使用 delete 和 truncate 截断表操作
delete from user; //delete,找到文件中表的存储空间,然后遍历数据删除
truncate table user; //truncate,找到文件中的数据存储空间,删除空间。truncate性能更好些
约束
约束是在表上强制执行的数据校验规则,约束主要用于保证数据库里数据的完整性
数据库支持五种完整性约束:
- not null:非空约束
//非空约束只能使用列级约束
alter table user
modify telephone varchar(30) not null;
- unique:唯一约束
//表级约束 使用表级约束建立唯一约束,同时指定约束名为namePass
alter table user add(
constraint namePass unique(userName.userPass)
)
- primary_key:主键约束
1、用于标识该表中的一条记录
2、主键不允许为空,也不允许出现重复值
3、主键采用primary key,既可以采用列级语法,也可以采用表级语法。采用表级语法时,虽然可以指定名称,但是不起作用,在数据库对象中仍然使用primary表示主键名称
//删除主键约束:如果是自增长的主键,需要先取消自增长,才能删除约束;对于非自增长的主键,主键约束可以直接删除
alter table user drop primary key;
- foreign_key:外键约束
//外键约束的建立有两种方式
//1、创建表结构时,建立外键约束
create table orders(
orderId int(11) primary key auto_increment comment '订单主键',
userId int(11) comment '用户外键',
price decimal(11,2) comment '订单总价',
orderDate date comment '订单生产日期',
peyment tinyint(4) comment '0未付款,1付款',
constraint fk_user_userId foreign key(userId) references user(userId);
)
//2、创建表结构后,单独追加外键约束
create table order_tail(
detailId int(11) primary key auto_increment comment '明细主键',
orderId int(11) comment '订单外键',
goodsId int(11) comment '商品外键',
buyNum int comment '购买数量'
)
alter table user add
constraint fk_orders_orderId foreign key(orderId) references orders(orderId);
//外键约束的删除:drop
//创建外键约束时,数据库系统会自动生成一个索引,所以想要彻底消除外键约束的影响,不仅要删除外键约束,还要额外删除索引
alter table orders drop foreign key fk_user_userId;
alter table orders drop index fk_user_userId;
注:外键约束建立之后,外键约束的列的数据必须是其引用约束表中已经存在的数据,才可以添加成功
- check:检查约束
索引
建立索引需要遵循的规范 : 1、该表中经常会被当做查询条件的列,建立索引
2、如果该表多个字段都经常会当成查询条件,对它们建立共同索引
索引的作用:快速查询机制,用于减少数据库的IO操作,能快速定位数据的物理位置,相当于书的目录机制一样
缺点:随着表中数据记录的值的增多,会影响表的增、删、改操作
//对于单列建立和删除索引
create index nikeNameIndex on user(nikeName);
drop index nikeNameIndex on user;
//对于多列建立索引
create index name_and_phone on user(nikeName, telephone);
DML语句——数据库操作语句
1、insert插入语句
语法:insert into tableName(columnName) values(value,value,...);
//如果不选择具体列的话,则要输入这一行中所有列要插入的数据
insert into user values(null, '张三', '123456', null, '男');
insert into user(userName, userPass) values('李四', '123654');
2、update更新语句
对表中已经存在的数据进行修改操作,可以一次性修改一列或多列,也可以一次性修改一行或多行
//一次性修改多行、多列
update user set userName='宋七', userPass='123321' where userName='张三' or userName='李四';
3、delete删除语句
- 物理删除:指在数据表中,将数据清除,在物理存储空间中将数据抹除
delete from user where userId=1 or userId=2; //删除user表中userId为1和2的行
delete from user where userId in (3,4,5); //删除user表中userId为3、4、5的行
- 逻辑删除:并不是真正是删除,在表中添加一列标识字段,用来标识该行是否可以被引用
4、selete查询语句
4.1、单表查询,查询所有数据,所有列
select * from user;
4.2、单表查询,查询所有数据,指定列
select userId,userName,userPass from user;
4.3、单表查询,查询指定列,指定数据,需要加上过滤条件
select * from user where userName like '%三%'; //查询user表中用户名含有“三”的行
4.4、多个条件组合查询
select * from user where userId>=2 and userId<=4; //查询在user表中userId介于2和4之间的行
select * from user where userId between 2 and 4; //同上
select * from user where userId<>3; //查询在user表中userId不等于3的行
4.5、单表查询,对于列名的处理
select userName name from user; //查询user表中列名为userName的列,并将其列名在显示时改为name
4.6、单表查询,对于null值的处理,在MySQL中null比较特殊,null不等于null,采用关键字is进行判断过滤
select * from user where telephone is null;
4.7、单表查询,过滤条件取非操作,不等于 <> 或 not
select * from user where userId not in(3); //查询user表中userId不为3的行
多表联合查询
92规范的书写格式:语法简单,但是结构不清晰
select u.userName, u.userPass, o.orderDate from user u, orders o
where u.userId=o.orderId and u.userName='宋七';
99规范的书写格式:结构清晰,语法稍微复杂些
select u.userName, u.userPass, o.orderDate from user u
join orders o on u.userId=o.orderId
where u.userName='宋七';
嵌套查询
嵌套查询:使用select语句进行查询后的结果当做一个查询条件,嵌套使用在另外一个sql语句当中,就是嵌套子查询
在演示嵌套查询前,先介绍三个新的知识点:组函数、分组函数、升序降序
- 组函数:sum,将多条记录数进行运算后,得到一个结果输出
select sum(buyNum) total from order_tail;
- 分组函数:group by
select sum(buyNum) total, goodsId from order_tail group by goodsId;
//将order_tail表中goodsId相同的行归为一组,并计算出各组buyNum的和
- 升序或降序操作:order by 列名 asc/desc(升序/降序)
select * from user order by userId desc;
介绍完这两个知识点之后,就是嵌套查询了
//显示商品名称和购买总数
select g.goodsName, c.total from goods g
join (select sum(buyNum) total,goodsId from order_tail group by goodsId) c on
g.goodsId=c.goodsId order by c.total asc;
视图
视图只是数据表中数据的逻辑显示,即一个查询结果
创建视图:create or replace view 视图名称 as 查询结果集
create or replace view sellView as
(select sum(buyNum) total, goodsId from order_tail group by goodsId);
删除视图:drop view 视图名称
drop view sellView;
注:视图只能查询,不能修改,不是物理数据,只是逻辑显示