MYSQL -外键索引

9:59 2019/3/8/周五
//////////////////////////
    数据库之间的约束
//////////////////////////
数据库之间的关联数据库(通过外键来约束)
比如一个表中的key_id和另一个表classcharge的id之间有关联,这个时候应该在里面加入{

      foreign key(key_id) references classcharge(id),
}
ps(切记:作为外键一定要和关联主键的数据类型保持一直。)
//create TABLE  student2(

       id TINYINT primary key auto_increment ,
       name varchar(20),
       charge_id tinyint,
       gender char(3),
       salary float(7,2),
       depart  varchar(20),
        Foreign key (charge_id )references classcharge(id)
   )engine =innodb;

//插入元素:
 insert into student2(name,charge_id,gender,salary,depart) values ("ccw",1,"女",9600,"商务英语"),
                                                            ("cm",2,"男",9600,"技术部"),
                                                            ("qz",2,"男",9600,"技术部"),
                                                            ("ann",1,"男",9600,"商务英语");
    update emp set salary=salary+2000 WHERE name="ann";
    delete from emp where id=4;
//创建表格
create  table classcharge(
         id tinyint PRIMARY KEY  auto_increment,
         name varchar(20),
         age int
 )
//插入元素:
 insert into classcharge(name,age) values ("小明",22),
                                          ("小刘",22)
//建立外键后不能删除元素。
mysql> delete from classcharge where id=1;

1451 - Cannot delete or update a parent row: a foreign key constraint fails (`demo`.`student2`, CONSTRAINT `student2_ibfk_1` FOREIGN KEY (`charge_id`) REFERENCES `classcharge` (`id`))
//要想删除,就必须把该元素关联的表格信息清空或者之间没有关联
:通过update来更新:
mysql> update student2 set charge_id =2 where id=1;
//创建外键联系:
mysql> alter table student3 add constraint abc
                     foreign key (charge_id)
                      references classcharge(id);

//删除联系:
alter table student drop foreign key abc;
/


////////////////////////////////
          级联删除
///////////////////////////////
create TABLE  student3(

       id TINYINT primary key auto_increment ,
       name varchar(20),
       charge_id tinyint,
       gender char(3),
       salary float(7,2),
       depart  varchar(20),
     Foreign key (charge_id )references classcharge(id) on delete cascade //这个里面加入了on delete cascade就是表示练级删除。
   )engine =innodb;
//多表查询:
连接查询:
内链接, inner join,
外链接,left join,
全链接。full join.
///笛卡尔集查询:
select *from emp,student2;
//在里面加入条件
 select *from emp,student2 where emp.name=student2.name;
//
(内联查询)
mysql> select *from student2 inner join emp on emp.name=student2.name;

//////////////////
修改唯一索引:unique

mysql> alter table student2 modify name varchar(20) unique;

/////////////////
自己创建索引:
create TABLE  index_name(

       id TINYINT primary key auto_increment ,
       name varchar(20),
       charge_id tinyint,
       gender char(3),
       salary float(7,2),
       depart  varchar(20),
       index index_name_id(name)//普通索引,index_name_id这个可加可不加。
                        
#       Foreign key (charge_id )references classcharge(id) on delete cascade
   )engine =innodb;
////唯一索引和普通索引就是唯一索引不能重复。

////添加索引:
mysql> create index index_name on student2(id);

//删除索引:
drop index index_name on student2;

 

你可能感兴趣的:(MySQL)