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', riqi date not null default '2012-01-01'
)charset utf8;
增删改查:案例分析
增:哪张表,哪列,值
insert into wolf (name,age,email,tel,riqi)
values (---);
主键值不能重复
insert into wolf
(name,age,tel)
values
('张飞',79,'66011123'),
('关羽',88,'66011124'),
('曹操',89,'66011125'); //一次可以插入多行
改
update wolf set id=2 name='wolf' where **
//修该多个值
select * from wolf where 1; //这个1为逻辑值’真‘
删:delete 只能删除行,alter可以drop列
delete from wolf where ** //
查:
安装ecshop 保留upload,重命名为ecshop
浏览器输入路径
localhost/ecshop
E:\MySql\ECSHop\upload
localhost/E:/MySql/Enviroment/PHPnow-1.5.4/MySQL-5.0.83/data/test
E:\MySql\Enviroment\PHPnow-1.5.4\MySQL-5.0.83\data\test
列类型
数值型
整形 tinyint,smallint,mediumint,intbigint
M,unsigned,zerofill
浮点型定点型float(m,d) unsigned,M精度,d小数位
decimal 比float更精确
字符型
char(M) 定长型,可存储的字符数
varchar(M),
日期时间类型
year 1901-2155,两位数输入法,
date yy-mm-dd ,范围1000-1-1》》9999-12-31
time HH:mm:ss 范围-838:59:59->838:59:59
datetime YY-MM-DD HH:ii:ss
1000-01-01
开发中的问题
时间戳,方便计算,格式成不同的样式
建表语句
create table 表名(
列名称列类型【列属性】【默认值】
)engine 引擎名 charset 字符集;
增:
insert into 表名
(列1,列2,列3)
values
(值1,值2,值3);
//如果不声明插入的列,则默认插入所有列
改:update 表名 set 列1=值1,列2=值2
where 表达式;//表达式
删:delete from 表名 where 表达式
查:select * from 表明