MySQL数据命令

时间:2021-07-09 16:59:04

一、数据库操作

创建数据库
create database db1 charset utf8;
查看数据库
show database;
show create database db1;
select database();
选择数据库
use db1;
删除数据库
drop database db1;
修改数据库
alter database db2 charset utf8;

二、表的创建与数据的插入

用户表
create table user(
id int,
user varcher() not null unique,
pwd varcher() not null
foreign key(group_id) references group(id)
);
用户表的添加
insert into user(user,pwd,group_id) values
('冬马和纱','',''),
('小母猪雪菜','',''),
('北原春希','',''),
('','',''); 用户组表
create table group(
id int primary key auto_increment,
group_name varcher()
);
多对多表
create table class2teacher(
id int primary key auto_increment,
class_id int not null,
teacher_id int,
constraint fk_class foreign key(class_id) references class,
constraint fk_teacher foreign key(teacher_id) references teacher,
unique(class_id,teacher_id)
);
insert into class2teacher(class_id,teacher_id) values
('',''),
('','');

MySQL数据命令

三、表查询

连表查询
select user.id,user.name,group_name from
user inner join group on user.group_id=group.id;
子查询
select user.name from user where group_id in
(select id from group where group_name='败犬组');