Mysql数据表的增删改查
---恢复内容开始---
Mysql数据表的增删改查
1.创建表
语法:CREATE TABLE 表名(字段1,字段2,字段3.......)
CREATE TABLE `users` ( `user_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `pid` mediumint(9) DEFAULT '0' COMMENT '介绍人', `role_id` mediumint(8) DEFAULT '0' COMMENT '分佣身份ID', `mobile` varchar(35) DEFAULT '' COMMENT '用户手机', `user_name` varchar(35) DEFAULT '' COMMENT '用户名', `password` varchar(32) DEFAULT '' COMMENT '用户密码', `pay_password` varchar(32) DEFAULT '' COMMENT '支付密码', `nick_name` varchar(20) DEFAULT '' COMMENT '昵称', `headimgurl` varchar(255) DEFAULT '' COMMENT '头像', `birthday` date DEFAULT NULL COMMENT '生日', `token` varchar(20) DEFAULT '' COMMENT '个人随机识别码', `signature` varchar(20) DEFAULT '' COMMENT '个性签名', `show_mobile` tinyint(1) DEFAULT '0' COMMENT '显示手机', `province` mediumint(8) DEFAULT '0' COMMENT '省份', `city` mediumint(8) DEFAULT '0' COMMENT '城市', `district` mediumint(8) DEFAULT '0' COMMENT '区域', `sex` tinyint(1) unsigned DEFAULT '0' COMMENT '性别', `reg_time` int(10) unsigned DEFAULT '0' COMMENT '注册时间', `total_consume` decimal(10,2) DEFAULT '0.00' COMMENT '累计消费', `is_bind` tinyint(1) DEFAULT '0' COMMENT '是否已执行绑定关系', `last_buy_time` int(10) DEFAULT '0' COMMENT '最近消费时间', `last_up_role_time` int(10) DEFAULT '0' COMMENT '最近升级会员时间', `login_time` int(10) DEFAULT '0' COMMENT '最近登陆时间', `login_ip` varchar(15) DEFAULT '' COMMENT '最近登陆IP', `last_login_time` int(10) DEFAULT '0' COMMENT '上次登陆时间', `last_login_ip` varchar(15) DEFAULT '' COMMENT '上次登陆IP', `is_ban` tinyint(1) DEFAULT '0' COMMENT '是否封禁(1是 0否)', `login_odd_time` int(10) DEFAULT '0' COMMENT '登陆异常时间', `login_odd_num` int(10) DEFAULT '0' COMMENT '登陆异常次数,超过10次限制登陆', `send_repeat_buy_msg_time` int(10) DEFAULT '0' COMMENT '发送复购提醒时间', `wx_id` varchar(50) NOT NULL COMMENT '微信号', `wx_nickname` varchar(50) DEFAULT '' COMMENT '呢称', `wx_code` varchar(255) DEFAULT '' COMMENT '微信二维码', PRIMARY KEY (`user_id`), UNIQUE KEY `token` (`token`) USING BTREE, KEY `pid` (`pid`) USING BTREE, KEY `mobile` (`mobile`) USING BTREE, KEY `nick_name` (`nick_name`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=29910 DEFAULT CHARSET=utf8 COMMENT='会员表';
 
2.修改、添加、删除 表字段
- 
添加表的字段 语法: alter table 表名 add 字段名 字段的类型例: alter table users add address varchar(110) after birthday; 
 批量怎加字段
 方法一:
 使用事务//事务开始 
 bagin;alter table em_day_data add f_day_house7 int(11); 
 alter table em_day_data add f_day_house8 int(11);
 alter table em_day_data add f_day_house9 int(11);
 alter table em_day_data add f_day_house10 int(11);
 commit;
 
 //提交事务,事务结束
 事务(transaction)是由一系列操作序列构成的程序执行单元,这些操作要么都做,要么都不做,是一个不可分割的工作单位。
 方法二
 mysql 批量为表添加多个字段
 语法:alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));
 例:alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));
- 
修改表的字段类型 语法: ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型定义;例: ALTER TABLE users MODIFY COLUMN login_ip VARCHAR(50); 
- 
修改表的字段名 语法:alter table 表名 change 原字段名 新字段名 字段的类型例: alter table student change address user_address varcher(100) default null 批量修改字段名称 
 语法:
 alter table 表 change 修改前字段名 修改后字段名称 int(11) not null,
 change 修改前字段名 修改后字段名称 int(11) not null,
 change 修改前字段名 修改后字段名称 int(11) not null,
 change 修改前字段名 修改后字段名称 int(11) not null,
 change 修改前字段名 修改后字段名称 int(11) not null
 例:
 alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
 change f_day_house12 f_day_hour12 int(11) not null,
 change f_day_house13 f_day_hour13 int(11) not null,
 change f_day_house14 f_day_hour14 int(11) not null,
 change f_day_house15 f_day_hour15 int(11) not null,
 change f_day_house16 f_day_hour16 int(11) not null,
 change f_day_house17 f_day_hour17 int(11) not null
- 
删除表的字段名 语法: alter table 表名 drop column 字段名例: alter table `users` drop column user_address 
- 
表添加索引 语法:alter table 表名 add index 索引名 (字段名1[,字段名2 …]);例: mysql> alter table users add index emp_name (username); 
- 
加主关键字的索引 语法: alter table 表名 add primary key (字段名);例: mysql> alter table users add primary key(id); 
- 
加唯一限制条件的索引 语法:alter table 表名 add unique 索引名 (字段名);例: mysql> alter table employee add unique emp_name2(cardnumber); 
- 
删除某个索引 语法: alter table 表名 drop index 索引名;例: mysql>alter table employee drop index emp_name; 
- 
添加注释// 可以为表添加注释 ALTER TABLE `table_name` COMMENT'注释'; // 为字段添加注释,同样适用于修改 ALTER TABLE `table_name` CHANGE `column_name` `column_name` type(longth) UNSIGNED NULL DEFAULT NULL COMMENT '注释' 
- 
调整字段顺序 语法:alter table 表名change 字段名 新字段名 字段类型 默认值 after 字段名(跳到哪个字段之后)alter table appstore_souapp_app_androidmarket; change getPriceCurrency getPriceCurrency varchar(50) default null AFTER getPrice; 
3.修改表名 语法: alter table 原表名 rename 现表名;
例: alter table t1 rename t2;
4.表数据的增删查改
-  (增)数据表新增记录  语法: insert into 表名 values(值1,值2,值3,....);
插入一条数据: 
 INSERT INTO `u_passive_friend` VALUES ('29890', '0', '0', '0', '0', '1558433128', '0'); INSERT INTO `u_passive_friend` VALUES ('29893', '0', '0', '0', '0', '1558433128', '0'); INSERT INTO `u_passive_friend` VALUES ('29894', '0', '0', '0', '0', '1558433128', '0'); INSERT INTO `u_passive_friend` VALUES ('29895', '0', '0', '0', '0', '1558433128', '0'); INSERT INTO `u_passive_friend` VALUES ('29896', '0', '0', '0', '0', '1558433128', '0'); INSERT INTO `u_passive_friend` VALUES ('29897', '0', '0', '0', '0', '1558433128', '0');
 同时插入多条数据
 格式:INSERT INTO 表名(字段名) VALUES(记录行值1),(记录行值2),...;INSERT INTO `u_passive_friend` VALUES 
 ('29897', '0', '0', '0', '0', '1558433128', '0'),('29895', '0', '0', '0', '0', '1558433128', '0'),('29896', '0', '0', '0', '0', '1558433128', '0'); 
- (删)删除数据 语法:DELETE FROM 表名[WHERE <condition>];
例: 
 DELETE FROM users WHERE user_id = 29897; //将users表中user_id=13的记录行给删除。
- (查)查询数据  语法:select 字段1,字段2,字段3,... from 表名 where  条件
例: select * from users; //查询users表所有数据 select user_id,nick_name,mobile from users;//查询users表所有数据的字段名user_id,nick_name,mobile select * from users where user_id>10000; //查询users中user_id大于10000的数据 select * from users where is_ban=0 order by add_time desc; //查询users中is_ban=0的所有数据,并根据add_time降序 
- (改)修改数据    语法:UPDATE 表名 SET 字段名=值,字段名=值... WHERE 条件;
例: UPDATE users SET nick_name = 'xxx' WHERE user_id='29896'; 
---恢复内容结束---
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号