Mysql笔记
- 格式:docx
- 大小:446.95 KB
- 文档页数:12
安装;基本命令
create table class (
stu int,
name varchar(20),
age int,
area varchar(20)
);
库
create database
show databases
drop database test
desc tables test //查看表结构
drop table test
desc test
rename table a to b //重命名表
库名不能改
增
insert into test
(id,age,name) //指定列
values
(1,10,'zhang3'); //单引号
set names gbk; //解决字符编码问题
insert into test
(age,name)
values
(10,'张三');
改
update test
set age=111,
name='liujichai'
where id=3;
列的默认值:
alter table test add age3 tinyint not null default 0;//取消null
删除
delete from test
where id=2;
alter table y drop column 列名//删除列
查
select id,name from test where id>3; //select,列,where,行
添加列
alter table test add age1 tinyint(3) zerofill;
create table class (
id int primary key auto_increment,
age tinyint
)charset utf8;
insert into test
(age,name,age1)
values
(20,'小王',3);
整形
tinyint 1字节
smallint 2
mediumint 3
int 4
bigint 8
tinyint(M) unsigned zerofill// 零填充,无符号
浮点型
float(M,D)//m总位数,d小数位占用4或者8个字节
create table goods (
name varchar(10) not null default ' ',
price float(6,2) not null default 0.00
)charset utf8;
定点型
alter table goods add max float(9,2) not null default 0.0;
alter table goods add min decimal(9,2) not null default 0.0;//定点型
insert into goods
(min,max)
values
(12.1111111111,13.222222222);
字符型
create table stu(
name char(8) not null default '', //定长,8个UTF-8字符,不够用空格补齐
aihao varchar(10) not null default ''//变长,用1-2个字节来记录占用了多少个字节
)charset utf8;
速度上,定长速度快char
text 型字符不能创建索引
日期类型
date 日期
time 时间
datetime 日期时间类型
year 年类型1901-2155
create table y (
ya year(4)
);
insert into y //不指定表,默认插入所有列values
('1901');
year类型,输入一位
输入两位
‘00-69’表示2000-2069
‘70-99’表示1970-1999
date型
1992-08-12
日期时间日期
create table d (
dt data);
time类型hh:mm:ss
insert into y (tm) values ('12:10:23'); datetime类型1989-05-06 14:32:05
时间戳
enum 枚举型
set 集合性
create table t2 (
gender enum('man','girl')
)charset utf8;
char set
例题:
name: char(3)
age: tinyint unsigned
email: varchar(30)
tel: char(11)
intro: varchar(100)
salary: decimal(7,2)
riqi: date
create table wolf (
id int primary key auto_increment,
name char(3) not null default '',
age tinyint unsigned not null default 0, email varchar(30) not null default '',
tel char(11) not null default '',
salary decimal(7,2) not null default '2000-01-01',