数据库一对一实例
drop database if exists tt;
create database tt;
usett;
drop table if exists tb_computer;
create table tb_computer(
idintauto_increment primary key,
namevarchar(50)
);
drop table if exists tb_student;
create table tb_student(
idintauto_increment primary key,
namevarchar(50) ,
c_idint UNIQUE
);
-- 修改外键
alter table tb_student ADD CONSTRAINT fk_student_computer FOREIGN KEY(c_id) references tb_computer(id) ;
-- 添加索引
create index indexfor_tb_student on tb_student(c_id);
drop database if exists tt;
create database tt;
usett;
drop table if exists tb_computer;
create table tb_computer(
idintauto_increment primary key,
namevarchar(50)
);
drop table if exists tb_student;
create table tb_student(
idintauto_increment primary key,
namevarchar(50) ,
c_idint
);
create table student_computer(
s_idint,
c_idint UNIQUE ,
primary key(s_id,c_id)
);
-- 修改外键
alter table student_computer ADD CONSTRAINT fk_scs
FOREIGN KEY(s_id) references tb_student(id) ;
-- 修改外键
alter table student_computer ADD CONSTRAINT fk_scc
FOREIGN KEY(c_id) references tb_computer(id) ;
-- 添加索引
create index indexfor_student_computer on student_computer(c_id,s_id);
drop database if exists tt;
create database tt;
usett;
drop table if exists tb_computer;
create table tb_computer(
idintauto_increment primary key,
namevarchar(50)
);
drop table if exists tb_student;
create table tb_student(
idint primary key,
namevarchar(50)
);
alter table tb_student ADD CONSTRAINT fk_student_computer FOREIGN KEY(id) references tb_computer(id);