MySQL基本操作

1、表约束

①非空约束   not  null

有非空约束的字段,insert的时候,必须添加,空字符不等于null。

mysql>create table tb1(

  ->id int,

  ->name varchar(20) not null                       

  ->);

添加非空约束

mysql> alter table tb1

  ->modify id int not null;

取消非空约束

mysql>alter table tb1

  ->modify id int;

②唯一约束   unique key

确保字段中的值唯一

mysql>create table tb2(

  ->id int unique key,

  ->name varchar(20)

  ->);

添加唯一约束

mysql>alter table tb2

  ->add unique key(name);

取消唯一约束

mysql>alter table tb2

   ->drop key name;

③主键约束  primary key

主键的作用: 可以唯一标识 一条数据,每张表里面只能有一个主键。

主键特性: 非空且唯一。当表里没有主键的时,第一个出现的非空且为唯一的列,被当成主键。

mysql>create table tb3(

   ->id int primary key;

   ->name varchar(20) not null

   ->);

取消主键

mysql>alter table tb3

  ->drop primary key;

添加主键

mysql>alter table tb3

  ->add primary key (id);

④自增长  auto_increment

auto_increment :自动编号,一般与主键组合使用。一个表里面只有一个自增默认情况下,起始值为1,每次的增量为1。

mysql> create table tb4(
   -> id int primary key auto_increment,
   -> name varchar(10)
   -> )auto_increment=50;     #指定自增长值从50开始

取消自增长

mysql>alter table tb4

   ->modify id int;

添加自增长

mysql>alter table tb4

   ->modify id int auto_increment;

指定自增长值

mysql>alter table tb4

   ->auto_increment=20;

⑤默认约束  default

default :初始值设置,插入记录时,如果没有明确为字段赋值,则自动赋予默认值。

mysql>create table tb5(

  ->id int primary key auto_increment,

  ->name varchar(10),

  ->age int default 18       #默认18岁

  ->);

取消默认

mysql>alter table tb5

  ->modify age int;

添加默认

mysql>alter table tb5

  ->modify age int default 16;

⑥外键约束  foreign key

外键约束 :保持数据一致性,完整性实现一对多关系。 外键必须关联到键上面去,一般情况是,关联到另一张表的主键

(因为一个表只存一类信息。用外键来做参照,保证数据的一致性,可以减少数据冗余)

mysql> create table a(
   -> a_id int primary key auto_increment,
   -> a_name varchar(10) not null
   -> );

mysql> create table b(
   -> b_id int primary key,
   -> b_name varchar(10),
   -> out_id int not null,

   ->constraint ab_id foreign key(out_id) references a(a_id)

   -> );

b表中的out_id 字段,只能添加 a_id中 已有的数据;a表中a_id  被参照的数据, 不能被修改和删除。

取消外键

mysql>alter table b

   ->drop foreign key ab_id;

添加外键

mysql>alter table b

   ->add constraint ab_id foreign key (out_id) references a(a_id);

2、表关系

①一对一关系

一对一 :用主键加主键的方式来实现这种关系,用外键把两个表的主键关联。

mysql>create table student(

   ->s_id int primary key,

   ->s_name varchar(10)

   ->);

mysql>create table details(

   ->d_id int primary key,

   ->sex varchar(10),

   ->age int,

   ->foreign key (d_id) references student(s_id)

   ->);

②一对多关系

通过外键关联来实现这种关系

mysql>create table college(

   ->c_id int unique key,

   ->d_name varchar(20) nor null

   ->);

mysql>create table student(

   ->s_id int primary key auto_increment,

   ->s_name varchar(10) not null,

   ->out_id int not null,

   ->constraint sc_id foreign key (out_id) references college(c_id)

   ->);

③多对多关系

多对多关系,需要创建中间表 实现。

mysql>create table student(

   ->s_id int primary key auto_increment,

   ->name varchar(10) not null

   ->);

mysql>create table course(

   ->c_id int primary key auto_increment,

   ->c_name varchar(20) not null,

   ->);

mysql>create table choose(

   ->s_id int,      #记录学生id

   ->c_id int,      #记录课程id

   ->primary key(s_id,c_id),         #联合主键

   ->foreign key(s_id) references student(s_id),       #关联学生id

   ->foreign key(c_id) references course(c_id)    #关联课程id

   ->);

 

posted @ 2018-05-02 17:17  轻风飞落叶  阅读(231)  评论(0编辑  收藏  举报