返回 mysql数据...... 登陆

mysql数据表创建、增删改查及关系表创建

瓶子 2018-12-14 14:15:15 322
-- 创建表
drop table if exists user;
create table user(
`id` mediumint(5) not null auto_increment comment '主键',
`name` varchar(100) not null default '' comment '用户名',
`sex` tinyint(2) comment '性别:0男;1女',
`age` tinyint(3) unsigned comment '年龄',
`email` varchar(200) not null default '' comment '邮箱',
`password` char(40) not null default '' comment '密码',
`status` tinyint(1) not null default 0 comment '用户状态:0未启用;1启用',
`create_time` int(10) not null default 0 comment '注册时间',
primary key(`id`) using btree,
unique index `name`(`name`) using btree
);
-- 插入
insert into `user` values(null,'杨康',0,26,'yk@php.com',sha1(123456),1,1543223803),(null,'欧阳克',0,25,'oyk@php.com',sha1(123456),1,1543223867);
insert into `user`(`name`,`sex`,`age`,`email`,`password`,`status`,`create_time`) values('华筝',1,18,'hzh@php.com',sha1(123456),1,1543225896);
-- 更新
update `user` set `age` = 26,`status` = 0 where id = 4;
-- 查询
select `name`,`sex`,`age`,`email`,`status` from `user` where `status` = 1 order by `age` desc limit 2;
-- 执行简单运算
select 15*2 `res`; 
-- 拼接字符串(直接拼接)
select concat(`id`,`name`) `user_str` from `user` where `status` = 1; 
-- 拼接字符串(两个拼接的字符串之间有自定义连接符号)
select concat_ws(':',`id`,`name`) `user_str` from `user` where `status` = 1; 
select count(`id`) `num` from `user` where `status` = 1; 
-- 删除
delete from `user` where `id` = 5;



当表中的某一字段在多条记录中有可能重复时可能造成资源占用或者影响查询,此时可以考虑将一张表分成多张表,用多张表的id进行关联,从而使查询更快,后期的扩展更容易。


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网