打开mysql
mysql -u root(用户名) -p;
创建与删除数据库
创建数据库
crate database test default character set utf8;
查看数据库:
show databases;
查看数据库编码:
select schema_name,default_character_set_name from information_schema.schemata
where schema_name = 'test';
select schema_name,default_character_set_name from information_schema.schemata
where schema_name = 'test';
删除数据库
drop database test(数据库名称);
进入数据库
选择数据库
use test(数据库);
MySQL中的数据类型
整数类型
| MySql数据类型 | 大小 |
|---|---|
| tinyint | 一个字节(-128~127) |
| smallint | 两个字节(-32768~32767) |
| mediumint | 三个字节(-8388608~8388607) |
| int | 四个字节(-2147483648~2147483647) |
| bigint | 八个字节(±9.22*10的18次方) |
zerofill
ZEROFILL 是 MySQL 的一个属性,用于将数值列的显示值用零填充,直至达到列定义中指定的显示宽度 参考此处
浮点类型
| MySql数据类型 | 大小 |
|---|---|
| float(a,b) | 单精度浮点 8位精度(4字节) a位总位数,b位小数 |
| double(a,b) | 双精度浮点 16位精度(8字节) |
字符类型
| MySQL数据类型 | 大小 |
|---|---|
| char | 固定长度,最多255个 |
| tinytext | 可变长度,最多255 |
| varchar | 可变长度 2*16 -1 |
| text | 可变长度 2*16 -1 |
| mediumtext | 可变长度,2*24 -1 |
| longtext | 可变长度,2*32 -1 |
可变长度和固定长度 :
固定长度 char 长度固定,每条数据占用等长空间,使用场景如身份证号,手机号码等
可变长度,可设置最大长度,使用长度可变的属性
text不设置长度,当不知道属性字段长度时,可设为text
常用 char 和varchar,varchar和text之间尽量使用varchar
日期类型
| 类型 | context |
|---|---|
| date | 日期YYYY-MM-DD |
| time | 时间 HH:MM:SS |
| datetime | date和time组合(YYYY-MM-DD HH:MM:SS) |
| timestamp | 时间戳 YYYYMMDD HHMMSS |
unix时间戳
Unix 时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数
UNIX时间戳的 0 按照 ISO 8601 规范为 :1970-01-01T00:00:00Z.
原理: 时间戳的主要目的在于通过一定的技术手段,对数据产生的时间进行认证,从而验证这段数据在产生后是否经过篡改
操作表
创建表和删除表
create table 表名(列名 类型,列名 类型 );
create table student (id int,name varchar(10), ranking int );
drop table student;
[!IMPORTANT]
默认值填充
create table student ( id tinyint primary key auto_increment, name varchar(10) , address varchar(50) default 'unknown' );
修改表名和列名
修改表名
alter table student rename students;
修改列名
alter table students change column ranking(旧列名) score_rank(新列名)int(类型);
查看所有列名
show full columns from students(表名);
show full fields from students(表名);
修改列类型
alter table students modify id char(10) ;
添加新列
alter table students add column diliveries int ;
删除指定列
alter table students drop column diliveries;
theme: github