一、数据库的操作
1.创建数据库
若在可视化软件上创建数据库,参考如下图
如果要创建的数据库不存在,则创建成功- create database if not exists westos;
复制代码
2.删除数据库
- drop database if exists westos;
复制代码
3.使用数据库
- use tmalldemodb;
- //tab键的上面,如果你的表名或者字段名是一个特殊字符,就需要带``
复制代码
4.查看数据库

5.清空当前命令行界面命令
6.创建表
- auto_increment:自增
- 字符串使用单引号括起来
- 所有语句后面加逗号,英文的,最后一个不用加
- primary key主键,一般一个表只有一个唯一的主键,且必须要有
- engine=innodb:数据库表的引擎
- mysql> create table if not exists student(
- -> id int(4) not null auto_increment comment '学号',
- -> name varchar(30) not null default '匿名' comment '姓名',
- -> pwd varchar(20) not null default '123456' comment '密码',
- -> sex varchar(2) not null default '女' comment '性别',
- -> birthday datetime default null comment '出生日期',
- -> address varchar(100) default null comment '家庭地址',
- -> email varchar(50) default null comment '邮箱',
- -> primary key(id)
- -> )engine=innodb default charset=utf8;
复制代码 创键成功之后的图

7.常用命令
(1)查看创建数据库的语句
- show create database 数据库名;
复制代码 运行结果图

(2)查看表的创建语句

(3)显示表的结构

7.数据表的类型
(1)数据库的引擎
- innodb //默认使用
- myisam//早些年使用的
复制代码
- 事务:假设有两个SQL都执行,要么都成功,要么都失败,如果有一个成功一个失败,它就提交不上去
- 数据行锁定:假设有两条SQL去查同一个表,他会把这个表先锁住,剩下的数据再查的时候就要排队等待
- 外键约束:一张表连接到另一张表
- 全文索引:比如在百度上根据你输入的关键词(在数据库叫字段)区搜索你想要的结果
myisaminnodb事务支持不支持支持数据行锁定不支持 (支持表锁)支持外键约束不支持支持全文索引支持不支持表空间的大小较小较大,约为myisam的2倍常规使用操作:
- myisam 节约空间,速度较快
- innodb安全性高,事务的处理,多表多用户操作
(2)在物理空间存在的位置
所有的数据库文件都存在data目录下本质还是文件的存储!
8.修改和删除表的字段
(1)修改表名
alter table 旧表名 rename as 新表名;- alter table teacher rename as teacher1;
复制代码
(2)增加表的字段
alter table 表名 add 字段名 列属性;- alter table teacher1 add age int(11);
复制代码
(3)修改表的字段(重命名,修改约束)
①修改约束modify(不能重命名):alter table 表名 modify 字段名 新的列属性;- alter table teacher1 modify age varchar(11);
复制代码
②字段重命名(既可以给字段重命名,又可以修改约束)
alter table 表名 change 旧字段名 新字段名 列属性;- alter table teacher1 change age age1 int(11);
复制代码
(4)删除表的字段
alter table 表名 drop 字段名;- alter table teacher1 drop age1;
复制代码
(5)删除表
如果要删除的表存在,则删除drop table if exists 表名;- drop table if exists teacher1;
复制代码
所有的创建和删除操作都尽量加上判断,以免报错
二、列的数据类型详解
1.数值
- tinyint 十分小的数据 1个字节
- smallint 较小的数据 2个字节
- int 标准的整数 4个字节 常用
- bigint 较大的数据 8个字节
- float 浮点数 4个字节
- double 浮点数 8个字节
- decimal 字符串形式的浮点数
2.字符串
- char 字符串固定的大小 0-255
- varchar 可变字符串 0-65535 常用
- tinytext 微型文本 2^8-1
- text 保存大文本 2^16-1
3.时间和日期
- date 日期格式:YYYY-MM-DD
- time 时间格式:HH:mm:ss
- datetime 日期格式:YYYY-MM-DD HH:mm:ss 常用
- timestamp 时间戳,从1970.1.1至现在的毫秒数 常用
- year 年份表示
4.null
- 没有值,未知
- 注意,不要使用NULL进行运算,结果为NULL
三、数据库的字段属性(重点)
1.unsigned
2.zerofill
- 0填充的
- 假设你现在要写一个长度为10的int类型,但是你只写了个1,则他会用自动给你在1前面填充9个零
3.自增
- 通常理解为自增,自动在上一条记录的基础上+1(默认)
- 通常用来设计唯一的主键~index,必须是整数类型
- 可以自定义设置主键自增的起始值和步长
4.非空
- 假设设置为not null ,如果不给它赋值,就会报错!
- Null,如果不填写值,默认就是null!
5.默认
- 设置默认的值
- sex,默认值为男,如果不指定该列的值,则会有默认的值!
- 设置默认的值
四、MySQL数据管理
1.外键(了解即可)

删除有外键关系的表的时候,必须要先删除字表,才能删除父表
明天7月17号继续
2.DML语言(全部背住)
3.添加
4.修改
5.删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |