肖蕾,3996,地底异形
创建数据库
create database [if not exists] haha;
删除数据库
drop database [if exists] haha;
使用数据库
use `haha`
查看数据库
show database --查看所有的数据库
学习思路
数值
tinyint 十分小的数据 1个字节
smallint 较小的数据 2个字节
mediumint 中等大小的数据 3个字节
int 标准的整数 4个字节
bigint 较大的数据 8个字节
float 浮点数 4个字节
double 浮点数 8个字节 (精度问题!)
decimal 字符串形式的浮点数 金融计算的时候,一般使用decimal
字符串
char 字符串固定大小的 0-255
varchar 可变字符串 0-65535 常用变量 类似与java中的string
tinytext 微型文本 2^8-1
text 文本串 2^ 16-1 保存大文本
时间日期
date yyyy-mm-dd 日期格式
time hh:mm:ss 时间格式
datetime yyyy-mm-dd hh:mm:ss 最常用的时间格式
timestamp 时间戳,1970.1.1到现在的毫秒数!也较为常用!
year 年份表示
null
没有值,表示未知
注意,不要使用null进行计算,结果会为null
unsigned:
zerofill:
自增
非空 not null 和 null
拓展:了解就好
/* 每一个表都必须存在以下五个字段,未来做项目用的,表示一个记录存在的意义 id 主键 version 乐观锁 is_delete 伪删除 gmt_create 创建时间 gmt_update 修改时间 */
-- 学号int 登录密码varchar(20) 姓名 ,性别varchar(2),出生日期(datatime),家庭住址,email -- 注意点,使用英文(),表的名称和字段尽量用``括起来 -- auto_increment 自增 -- 字符串使用单引号括起来 -- 所有语句后面加, (英文的),最后一个不用加 -- primary key 主键 ,一般一个表只有一个唯一的主键! create table if not exists `student`( `id` int(4) not null auto_increment comment '学号', `name` varchar(100) 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 '家庭住址', primary key(`id`) )engine=innodb default charset=utf8
格式
creat table [if not exists] `表名`( '字段名' 列类型 [属性] [索引] [注释], '字段名' 列类型 [属性] [索引] [注释], .... '字段名' 列类型 [属性] [索引] [注释] )[表类型][字符集设置][注释]
常用语句
show create database school --查看创建的数据库语句 show create table student -- 查看创建的数据库表定义 desc student --查看表结构
innodb(现在默认使用)和myisam(以前常用)区别
myisam | innodb | |
---|---|---|
事务支持 | 不支持 | 支持 |
数据行锁定 | 不支持 | 支持 |
外键约束 | 不支持 | 支持 |
全文搜索 | 支持 | 不支持 |
表空间的大小 | 较小 | 较大,约2倍 |
常规使用操作:
在物理空间存在的位置
所有数据库文件都存在 data目录下
本质还是文件存储
mysql引擎在物理文件上的区别
设置数据库表字符集编码
charset=utf8
不设置的话,会是mysql的默认字符集编码不支持中文
mysql 的默认编码是latin1,不支持中文
修改表
--修改表名 : alter table 旧表名 rebane as 新表名 alter table teacher rename as teacher1 -- 增加表的字段 : alter table 表名 add 字段名 列属性 aalter table teacher1 add age int(11) -- 修改表的字段 (重命名 ,修改约束!) -- alter table 表名 modify 字段名 列属性 [] alter table teacher modify age varchar(11) --修改约束 --alter table 表名 change 旧名字 新名字 列属性[] alter table teacher1 change age age1 int(1) -- 字段重命名 -- 删除表的字段: alter table 表名 drop 字段名 alter table teacher1 drop age1
删除表
-- 删除表(如果存在再删除) drop table if exists teacher1
所有创建和删除操作,尽量加上判断,以免报错
注意点:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
数据库SQL---数据库、基本表、视图、索引的定义、修改、删除
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (上)
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (下)
网友评论