MySQL的常用命令

前言

MySQL命令行操作db的常用命令,为了更好地展示命令行的效果,我将我的远程ip假设为10.83.29.246,端口号为17286,账号是root,密码为123456;创建的数据库名为testDb,创建的数据表名为testTable

  • 连接db
mysql -h10.83.29.246 -p17285 -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("tang","123456");
delete from 表名 [条件];
例:delete from testTable where userName="tang";
select*from 表名 [where 等语句]
例:select* from testTable where userName='tang' and pwd='123456';
update 表名 set 字段名=字段值 [条件];
例:update testTable set pwd='123' where userName='tang';
  • 删除表
drop table 表名;
例:drop table testTable;
  • 清空表
delete from 表名; (delete支持条件删除)
或
truncate table 表名;
  • 删除表中主键
alter 表名 drop primary key;
  • 为数据表添加主键
alter 表名 add primary key 字段;
alter 表名 add primary key (字段一,字段二.....);
  • 添加字段
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 右连接
      在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值