Mysql常用指令
一、mysql常用指令
1.创建数据库
2.创建数据表(含主键、索引、唯一索引)
3.建好表增加索引
4.插入记录 insert into
5.删除记录 delete
6.修改记录 update
7.查询记录 select
8.查询没有再添加,有则修改:replace into
二、具体操作(待完善)
1.创建数据库
create database if not exists db_test default charset utf8 collate utf8_general_ci;
2.创建数据表(含主键、索引、唯一索引)
create table tb_1(
n_id int not null auto_increment comment '自增变量',
c_userid varchar(20) not null comment '用户id',
c_phone varchar(11) not null comment '手机号码',
c_password varchar(20) not null comment '用户密码',
n_sex tinyint null default 0 comment '0为女,1为男',
primary key(n_id),
unique index ukey(c_userid),
index index_1(c_userid,c_phone)
)
3.建好表增加索引
create index index_2 on tb_1(c_phone,c_password);
4.插入记录 insert into
insert into tb_1(c_userid,c_password,c_phone) value("user1","123456","10086");
insert into tb_1(c_userid,c_password,c_phone) values("user2","123456","10086"),("user3","123456","10086"),("user4","123456","10086");
5.删除记录 delete
delete from tb_1 where c_userid ="user1";
6.修改记录 update
update tb_1 set c_password ='111111' where c_userid='user2';
7.查询记录 select
select c_userid,c_password,c_phone,n_sex from tb_1 where c_password='123456';
8.查询没有再添加,有则修改:replace into:
replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中, 1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 2. 否则,直接插入新数据。
要注意的是:插入数据的表必须有主键或者是唯一索引!否则的话,replace into 会直接插入数据,这将导致表中出现重复的数据。
replace into tb_1(c_userid,c_password,c_phone) values("user3","123456","10086");
replace into tb_1(c_userid,c_password,c_phone) values("user3","123456","10086")
> Affected rows: 2
> Time: 0.048s
9.判断符合要求的记录是否存在(例:判断user表内c_uid用户是否存在)
select 1 from user where c_uid = "user1" limit 1;
select 1 from user where c_uid = "user2" limit 1;