例:创建员工表tb_employee1,结构如下表所示
字段名称 | 数据类型 | 备注 |
---|---|---|
id | int(11) | 员工编号 |
name | varchar(25) | 员工名称 |
depld | int(11) | 所在部门编号 |
salary | float | 工资 |
mysql> create database aa;
mysql> use aa;
mysql> create table tb_employee1(id int(11),name varchar(25),depld int(11),salary float);
(1)单字段主键
mysql> create table tb_employee1(id int(11)primary key,name varchar(25),depld int(11),salary float);
mysql> create table tb_employee1(id int(11),name varchar(25),depld int(11),salary float,primary key(id));
(2)多字段联合主键
mysql> create table tb_employee1(id int(11),name varchar(25),depld int(11),salary float,primary key(id,name));
下面介绍几个概念。是表中的一个字段,它可以不是本表的主键,但对应另外一个表的主键。外键的主要作用是保证数据引用的完整性,定义外键后,不允许删除在另一个表中具有关联关系的主键。例如:部分表的主键id,在表tb中有一个键deptld与这个id关联。
主表(父表):对于两个具有关联关系的表而言,相关字段中主键所在的那个表就是主表。
从表(自表):对于两个具有关联关系的表而言,相关字段中外键所在的那个表就是从表。
例:定义数据表tb_employee5,并且在该表中创建外键约束
创建一个部门表tb_dept1,表结构如下表所示
字段名称 | 数据类型 | 备注 |
---|---|---|
id | int(11) | 部门编号 |
name | varchar(22) | 部门名称 |
location | varchar(50) | 部门位置 |
mysql> create table tb_dept1(id int(11)primary key,name varchar(22),location varchar(50));
定义数据表tb_employee5,让它的deptld字段作为外键关联到tb_dept1的主键id:
mysql> create table tb_employee5(id int(11)primary key,name varchar(25),deptld int(11),salary float,constraint fk_emp_dept1 foreign key(deptld) references tb_dept1(id));
例:定义数据表tb_employee6,指定员工的名称不能为空
mysql> create table tb_employee6(id int(11)primary key,name varchar(25)not null,deptld int(11),salary float);
(1)在定义完列之后直接指定唯一约束
语法规则:字段名 数据类型 unique
mysql> create table tb_dept2(id int(11)primary key,name varchar(22)unique,location varchar(50));
(2)在定义完所有列之后指定唯一约束
语法规则:constraint 约束名 unique(字段名)
mysql> create table tb_dept3(id int(11)primary key,name varchar(22),location varchar(50),constraint sth unique(name));
unique和primary key的区别:一个表中可以有多个字段声明为unique,但只能有一个primary key声明;声明为primary key的列不允许有空值,但是声明为unique的字段允许空值的存在。
语法规则:字段名 数据类型 default 默认值
例:mysql> create table tb_employee7(id int(11)primary key,name varchar(25)not null,deptld int(11)default 1111,salary float);
语法规则:字段名 数据类型 auto_increment
例:定义数据表tb_employee8,指定员工标号自动增加
mysql> create table tb_employee8(id int(11)primary key auto_increment,name varchar(25)not null,deptld int(11),salary float);
mysql> insert into tb_employee8(name,salary) values('lucy',1000),('lii',800),('cai',20000);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from tb_employee8;
+----+------+--------+--------+
| id | name | deptld | salary |
+----+------+--------+--------+
| 1 | lucy | NULL | 1000 |
| 2 | lii | NULL | 800 |
| 3 | cai | NULL | 20000 |
+----+------+--------+--------+
3 rows in set (0.00 sec)
查看表基本结构:describe 表名 或 desc 表名
mysql> desc tb_employee8;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(25) | NO | | NULL | |
| deptld | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
(1)NULL:表示该列是否可以存储null值
(2)key:表示该列是否已经编制索引。pri为该列主键的一部分;uni表示该列是unique索引的一部分;mul表示在列中某个给定值允许出现多次。
语法规则:show create table 表名\G
mysql> show create table tb_employee8\G
*************************** 1. row ***************************
Table: tb_employee8
Create Table: CREATE TABLE `tb_employee8` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`deptld` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
(1)修改表名
alter table 旧表名 rename 新表名
例:mysql> alter table tb_dept3 rename tb_deptment3;
(2)修改字段数据类型
alter table 表名 modify 字段名 数据类型
例:mysql> alter table tb_dept1 modify name varchar(30);
(3)添加字段
alter table 表名 add 新字段名 数据类型
例1:mysql> alter table tb_dept1 add managerld int(20);(没有完整性约束条件的字段)
例2:mysql> alter table tb_dept1 add column1 varchar(12) not null;(添加有约束条件)
例3:mysql> alter table tb_dept1 add column2 int(11) first;(添加到第一列)
例4:mysql> alter table tb_dept1 add column3 int(11) after name;(添加到name后一列)
(4)删除字段
alter table 表名 drop 字段名
例:mysql> alter table tb_dept1 drop column3;
(5)修改字段排序
alter table 表名 modify 字段1 数据类型 first after 字段2
例1:mysql> alter table tb_dept1 modify id int(11) first;
例2:mysql> alter table tb_dept1 modify column2 int(11) after column1;
(6)更改表的数据引擎
alter table 表名 engine=更改后的存储引擎
例:mysql> alter table tb_dept1 engine=innodb;
(7)删除表的外键约束
alter table 表名 drop foreign key 外键约束名
(8)修改字段名
mysql> alter table customers change c_contact c_phone varchar(50);
drop table 表1 表2 。。。
删除被其他表关联的主表:先删除字表,再删除主表(或取消外键约束,删除主表)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有