mysql命令行操作db的常用命令,为了更好地展示命令行的效果,我将我的远程ip假设为10.83.29.246,端口号为17286,账号为root,密码为123456;创建的数据库名为testDb,创建的数据表名为testTable。
- 连接db
mysql -h10.83.29.246 -P17286 -uroot -p123456
- 查看账号下所有的数据库
show databases;
- 创建数据库
create database 数据库名;
例: create database testDb;
- 删除数据库
drop database 数据库名;
- 进入某数据库
use 数据库名;
例: use testDb;
- 查看数据库中的数据表
show tables;
- 创建数据表
create table 表名(字段列表);
例: create table testTable(id INTEGER(5) primary key AUTO_INCREMENT, userName VARCHAR(256) not null, pwd VARCHAR(256) not null);
- 删除数据表
drop table 表名;
- 查看数据表结构
describe / desc table 表名;
例: describe / desc table testTable;
-
CRUD(增、查、改、删)基本操作
- 增
insert into 表名(字段列表) values(值列表); 例: insert into testTable(userName, pwd) values("qiuqiu", "123456");
- 查
select * from 表名 [where 等语句]; 例: select * from testTable where userName='qiuqiu' and pwd='123456';
- 改
update 表名 set 字段名=字段值 [条件]; 例: update testTable set pwd='123' where userName='qiuqiu';
- 删
delete from 表名 [条件]; 例: delete from testTable where userName='qiuqiu';
-
删除表
drop table 表名;
例: drop table testTable;
- 清空表
delete from 表名; (delete支持条件删除)
或
truncate table 表名;
- 删除表中主键
alter 表名 drop primary key;
- 为数据表添加主键
alter 表名 add primary key 字段;
alter 表名 add primary key (字段1, 字段2, ...);
- 添加字段
alter table 表名 add 字段名 属性;
- 删除字段
alter table 表名 drop 字段名;
- 修改字段类型
alter table 表名 modify 字段 属性;
例: alter table testTable modify pwd VARCHAR(20) not null;
- 修改字段名
alter table 表名 change 原字段名 新字段名;
- 修改字段默认值
alter table 表名 alter 字段名 set default 默认值;
-
连接,具体可查看 => 菜鸟教程详解
- inner join 内连接或等值连接
- left join 左连接
- right join 右连接