创建库
create database [ if not exists ] `库名`;
创建数据表
create table [ if not exists ] `表名`(
`字段名` 数据类型 数据属性,
`字段名` 数据类型 数据属性,
`字段名` 数据类型 数据属性,
...
`字段名` 数据类型 数据属性 (最后一个字段分隔符, 不能加)
)engine=数据库引擎 default charset=编码类型;
数据属性:
1. 主键索引 primary key
(唯一,一张表推荐一个主键)
2. 唯一索引 unique
(唯一)
主要目的 : 避免数据重复 , 附带提高查询速度
缺点 : 占用磁盘空间
3. 自增 auto_increment
4. 非空约束not null
(不能为空)
5. 默认值 default 值
注: 数据属性不写, 默认为 null
6. 描述 comment '描述内容'
7. 无符号 unsigned
(非负限定 ,即不能取负值) 取值范围 0–255
数据表(示例–用户信息表)
create table if not exists `user_info`(
`id` int auto_increment primary key comment '用户id [自增 |主键]',
`name` varchar(30) not null comment '用户名[不能为空]',
`password` char(32) not null comment '密码[不能为空]',
`sex` tinyint(1) default 1 comment '性别 1-男 2-女[默认为1]',
`email` varchar(50) comment '邮箱',
`tel` char(11) not null unique comment '电话[唯一]',
`address` varchar(255) comment '住址',
`birthday` date comment '生日',
`status` tinyint(1) default 1 comment '状态 1-激活 2-禁用',
`regtime` int not null comment '注册时间'
)engine=innoDB default charset=utf8;
删除一张表
---- drop 删结构;
---- delete 删数据;
-- delete from `表名`;
delete from `user`;
-- drop table `表名` ;
drop table `user`;
查看表结构
desc `表名`;
查看建表语句
show create table `表名`;
插入数据
-- insert into `表名` values(所有的字段值);
-- insert into `表名`(指定的字段名) values (对应的字段值),(),(),....;
更新数据
-- update `表名` set `字段名1` = '值1', `字段名2` = '值2',...;
update `user` set `sex` = 2;
-- update 表名 set 字段名1 = 字段值1, 字段名2 = 字段值2, ... where 条件表达式
update `user` set `sex` = 1 where `status` = 2;
查询公式
select 字段名
[from 表名]
[where 条件表达式]
[group by 字段名]
[having 条件表达式]
[order by 字段名]
[limit 下标, 行数]
-- 查询表中所有的数据
select * (注意点: 能不用*则不用, 影响性能) from `user`;
-- 带条件查询
select `name , sex ` from `user` where `id`=1;
-- is null
select name from user where address='洛阳';
-- between 范围查询
select name from uaer where id between 1 ,10; #查询出id为1-10的数据
select name from uaer where id between 1 and 10; #查询出id为1 和 10的数据
-- like 模糊搜索
select name from user where name like "%亮"; # 值结尾为 亮
select name from user where name like "%亮%"; # 值包含 亮
select name from user where name like "%亮%"; # 值开头为 亮
-- in 范围判断 in_array
select id, name from user where id in(1,2,3);
聚合函数(不建议使用函数)
-- count 统计总个数
select count(`id`) from `user`;
-- sum 求和
select sum(`id`) from `user`;
-- max 最大
-- min 最小
select max(`id`) from `user`;
-- avg 平均值
select avg(`id`) from `user`;
-- 字符串拼接
select concat(`id`, '------', `name`, '-------------------', `tel`) from `user`;
select now()
select UNIX_TIMESTAMP()
select version()
修改表名
-- alter table 旧表名 rename 新表明
ALTER TABLE user RENAME user1;
修改字段的数据类型
-- alter table 表名 modify 字段名 新数据类型
ALTER TABLE user1 MODIFY PHONE CHAR(20);
修改字段名
-- ALTER TABLE `表名` CHANGE `旧字段` `新字段` 新数据类型
ALTER TABLE user1 CHANGE phone tel char(11);
增加字段名
-- alter table 表名 add 字段名 数据类型 字段属性
ALTER TABLE `user1` ADD `name` varchar(30) not null;
-- alter table 表名 add 字段名 数据类型 字段属性 after 字段名
ALTER TABLE `user1` ADD `name` varchar(30) not null after `nickname`;
-- alter table 表名 add 字段名 数据类型 字段属性 first;
ALTER TABLE user1 add name varchar(30) not null first;
删除字段
-- alter table 表名 drop 字段名;
alter table `user1` drop `name`;
修改字段的排列位置
-- alter table 表名 modify 字段名 新数据类型 first
ALTER TABLE user1 MODIFY phone char(90) first;
-- alter table 表名 modify 字段名 新数据类型 after 字段名
ALTER TABLE user1 MODIFY phone char(90) after nickname;
更该表的储存引擎
alter table `表名` engine=数据表引擎;
ALTER TABLE `user1` ENGINE=INNODB;
查看表的储存引擎
show create table 表名;
SHOW CREATE TABLE `user1`;
数据表引擎 MyISAM 和 InnoDB 区别
MyISAM 和 InnoDB
1. CURD 增删改查
m: 在执行大量的查询操作, 推荐myisam
i: 在执行大量的增删改操作, 推荐innodb
2. 事务
m: 不支持, 追求的效率
i: 支持, 追求的功能, 其他的高级数据库
3. 全文索引 FULLTEXT
m: 支持
i: 不支持, 通过第三方技术, sphinx
4. 表总行数
m: 存储总行数 , 通过count直接获取总行数
i: 没有存储总行数, 通过count一行一行的遍历, 非常消耗资源
注意点:
如果统计总行数时, 带了条件, 那么两个引擎毫无区别, 都是一行一行统计.
5. 行锁, 表锁
m: 支持表锁
i: 支持表锁 和 行锁(默认)