
打开navicat工具,连接上mysql服务器,选择完数据库之后,选择一个表右击选择设计表(这里为了演示测试,随便选择一个表即可)

在设计表页面,可以看到当前表的所有字段信息,我们选择的学生表有一个id字段,目前该表没有主键字段。

如图,在最后一列右击选择主键,即可将该字段设置为主键,也可以直接点击鼠标左键,可以快速添加和取消主键。

设置完主键之后,可以看到一把锁的标识,并且有一个1字,因为一个表可以给多个字段添加主键,则为联合主键,这样就显示为主键1,主键2等。

主键设置完成之后,还没有自增,选择id字段之后,在下方,如图,勾选自动递增,这样id在每次插入记录之后都会自增一个值。

设置完成主键和自增之后,点击保存,关闭当前窗口,然后选择表名右击选择对象信息。

在DLL页面中,可以看到刚刚添加的主键和自增的DLL语句,这里就是创建表的DLL语句。

上面有提到联合主键,其实一个表可以给多个字段设置主键,这样可以组成联合主键,对于特定的业务,联合主键也是必须的。

网友评论