day34-mysql数据表操作、数据行操作

数据表的操作

/*
create table 表名(
字段名 数据类型 [可选参数],
字段名 数据类型 [可选参数]
) charset=utf-8;
*/

列约束可选参数

-- auto_increment 	自增
-- primary key 		主键
-- not null 		标识该字段不能为空
-- default 			为该字段设置默认值

数据类型

整型

-- tinyint   1字节	
-- smallint	 2字节
-- mediumint 3字节
-- int 		 4字节
-- bigint	 8字节
# unsigned 无符号,默认有符号

浮点型

-- float			4字节
-- double			8字节
-- decimal(M, D)	如果M>D为M+2;否则D+2(M为整体长度,D为小数部分长度)
# decimal的精度远大于float

字符串

-- char(len) 			定长(len)
-- varchar(len)			变长(字符长度+1)

char:定长,无论插入多少字符,永远固定占规定的长度

varchar:变长,根据插入的字符串长度来计算占的字节数,有一个字节是用来保存字符串的大小的

如果不确定插入数据的大小,建议使用varchar(255)

时间日期类型

-- year 		YYYY					1901/2155
-- date			YYYY-MM-DD				1000-01-01/9999-12-31
-- time			HH:MM:SS				'-838:59:59'/'838:59:59'
-- datetime		YYYY-MM-DD HH:MM:SS		1000-01-01 00:00:00/9999-12-31 23:59:59
-- timestamp	YYYYMMDD HHMMSS			1970-01-01 00:00:00/2037 年某时

枚举

/*
create table 表名(
id int auto_increment primary key,
gender enum('male', 'female')
);
*/

create database test;  #创建数据库

use test;  #使用数据库

create table t1(  #创建数据表
id int auto_increment primary key,  #自增字段,在删除记录后,会在原来的基础上自增
name varchar(255)
);  

alter table t1 modify name char(20);  #修改字段属性

alter table t1 change name name1 char(20);  #修改字段名和属性

alter table t1 add name1 char(20);  #增加字段
alter table t1 add name1 char(20) first;
alter table t1 add name1 char(20) after name;

alter table t1 drop name1;  #刪除字段

插入记录

/*
insert into 表名(列1, 列2,...) values
(值1, 值2,...),
(值1, 值2,...);
*/

查询记录

# select 列1, ... from 表名 where 查询条件;
# select * from 表名 where 查询条件;	查询所有列

/*
drop table 表名;  线上禁用
*/

show tables;

复制表结构

show create table test; #查看表结构 1复制创建语句

create table t1 like test; #2

数据行操作

-- insert into 表名(列1, 列2,...)values(值1, 值2,...)

-- delete from 表名 where 条件; 按条件删除
-- delete from 表名; 			 删除表中所有数据
-- truncate 表名; 			 删除表中所有数据

delete 删除全表:插入数据从上一次主键自增开始,truncate从1开始

delete 删除全表是一行一行的删除;truncate是全选删除,速度大于delete

-- update 表名 set 列1=值1,列2=值2,... where 条件;

-- select 列 from 表名; 
-- select * from 表名;

-- between..and.. 闭区间
-- distinct 去重
-- like 模糊查询 %表示任意长度

posted on 2019-10-29 16:02  shenblogs  阅读(124)  评论(0)    收藏  举报

导航