Mysql数据库常用命令

数据库:
查看数据库: show database;
创建数据库:create database 库名;
使用数据库:use 库名;
删除数据库:drop database 库名;

表:
创建表:
在这里插入图片描述
create table 表名 ();

create table stu(
id int
name varchar(20)
);
create table stu(
id int auto_increment primary key comment’主键’ ,
name varchar(20) not null comment ‘姓名’ ,
phone varchar(20) comment’电话’
);

添加字段:
alter table 表名 ;
alter table stu add name varchar(10)【first,after id】;

删除字段:
alter table stu drop 字段名;

主键:
主键的创建
alter table class add primary key(profess_id);
主键的删除
alter table students drop primary key;

创建外键:
alter table pro add foreign key(bianhao) references students(stu_id);
为 pro 中 bianhao 创建外键,至students 中的 stu_id ,且 stu_id 必须为主键
alter table class add 【constraint fk_id 】foreign key(profess_id) references studendts(stu_profess_id); -----【】中的可以省略

对数据的操作:
插入数据:
insert into 表名 ( 字段名,字段名)values
(值1,值2,值3);----这里的字段可以和表中字段顺序不一致,但是此处的字段名和值 必须对应。
在插入顺序与表顺序一致的情况下,可以省略字段名
insert into 表名 values
(值1,值2,值3); ------值为空 ,可以用 null 代替

修改数据:
update stu set name = ‘tom’ where stu_id = ‘002’----
alter table students change column classID classID … ;

删除数据:
delete from stu where id = 1
delete from stu where id < 5
delete from stu ;
没有条件时删除所有数据
删除对应的数据

查找数据:
select * from stu ;
显示表中的所有列
select stu_id,stu_name from stu ;
显示表中指定的列
select * from stu where stu_id = ‘003’ ;
精确查找,其中where后面可以接逻辑语句

基础知识:
详情见转载:https://blog.csdn.net/weixin_44392716/article/details/104191623

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值