MySQL常用命令

MySQL常用命令

命令含义
mysql -uroot -p进入MySQL,需要输入密码
show databases查看有哪些数据库
create database xxx创建数据库
drop database xxx删除数据库
use xxx使用该数据库
show tables查看数据库中有哪些表
  • 创建表
 #创建表(eg:student)
 mysql> create table student
    -> (
    -> id int not null,
    -> name varchar(45),
    -> age int not null,
    -> pwd varchar(45) not null,
    -> primary key(id)
    -> );
  • 查看student 的表结构
desc student
  • 更新数据库表的结构
alter table student add major varchar(20)更新表的定义,给表加上一列使用
alter table student drop column age删除表中的一列,用到关键字column
alter table student modify 字段名称 字段类型【是否非空】修改表属性类型;(*)
delete from student删除表中所有内容(法:1)
truncate table student删除表中所有内容(法:2)
drop table student删除表
delete from student where id = x删除表的一条记录
alter table student change<字段名><字段新名称><字段的类型>**[新增命令]**修改字段的名称

往往在网站进行重构时,通常会进行数据结构的修改。

#批量增加字
#可以使用事务
begin;   #事务开始
alter table 表名 add 字段名 字段类型(长度);
alter table 表名 add 字段名 字段类型(长度);
alter table 表名 add 字段名 字段类型(长度);
alter table 表名 add 字段名 字段类型(长度);
commit;

#或者
alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));
  • 增(插入语句)
insert into student(id,name,pwd,major) values (1,‘chulan’,‘12345’ ,‘computer’)
  • 查看
select * from student
  • (id)属性自增操作(*)
id int not null auto_increment
  • 使用update语句更新某一列内容
update 表名 set 属性1=新值1,属性2=新值2 where 属性3 = xxx
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值