Mysql数据库常用命令

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

表:
创建表:
Mysql数据库常用命令
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后面可以接逻辑语句