mysql数据库表的基本操作

本文介绍了MySQL中创建表、添加外键约束、定义唯一性约束、设置表属性(自动增加)、查看表详细结构、修改表名、字段数据类型、字段名等关键数据库操作与表管理技巧。

1.外键关联:

mysql> create table tb_dept
    -> (id int(11) primary key, name varchar(22) not null, location varchar(50));
Query OK, 0 rows affected (0.11 sec)


mysql> create table tb_emp5
    -> (id int(11) primary key, name varchar(25), deptId int(11), salary float, constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id));
Query OK, 0 rows affected (0.01 sec)


mysql> describe tb_emp5;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(25) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  | MUL | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

以上的例子,再表tb_emp5上添加了名称为fk_emp_dept1的外键约束,外键名称为deptId,其依赖于表tb_dept1的主键id。

2.定义唯一性约束

两种方法;

第一:

mysql> create table tb_dept2
    -> (id int(11) primary key, name varchar(22) unique, location varchar(50));
Query OK, 0 rows affected (0.02 sec)

第二:

mysql> create table tb_dept3 (id int(11) primary key, name varchar(22), location varchar(50), constraint sth unique(name));
Query OK, 0 rows affected (0.01 sec)


mysql> describe tb_dept3;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(22) | YES  | UNI | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

3.设置表的属性值自动增加

mysql> create table tb_emp8 (id int(11) primary key auto_increment, name varchar(25) not null,  deptId int(11), salary float);
Query OK, 0 rows affected (0.01 sec)


mysql> insert into tb_emp8(name, salary)
    -> values('Lucy', 1000), ('Lura', 1200), ('Kevin', 1500);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> select * from tb_emp8;
+----+-------+--------+--------+
| id | name  | deptId | salary |
+----+-------+--------+--------+
|  1 | Lucy  |   NULL |   1000 |
|  2 | Lura  |   NULL |   1200 |
|  3 | Kevin |   NULL |   1500 |
+----+-------+--------+--------+
3 rows in set (0.00 sec)

从上面的例子看出,增加三条记录,并没有输入id的值,但是系统自动添加该值。

4.查看表详细结构语句show create table

mysql> show create table tb_emp8;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_emp8 | CREATE TABLE `tb_emp8` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  `deptId` 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)

加上\G后,可使显示的結果更加直观

mysql> show create table tb_emp8 \G;
*************************** 1. row ***************************
       Table: tb_emp8
Create Table: CREATE TABLE `tb_emp8` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  `deptId` 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)


ERROR: 
No query specified

5.修改表名

mysql> alter table tb_dept3 rename tb_deptment3;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_dept           |
| tb_dept2          |
| tb_deptment3      |
| tb_emp2           |
| tb_emp3           |
| tb_emp4           |
| tb_emp5           |
| tb_emp8           |
| tb_empl           |
+-------------------+
9 rows in set (0.00 sec)

6.修改字段数据类型

mysql> desc tb_dept2;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(22) | YES  | UNI | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table tb_dept2 modify name varchar(30);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(30) | YES  | UNI | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

7.修改字段名

mysql> desc tb_dept2;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(30) | YES  | UNI | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table tb_dept2 change location loc varchar(50);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(30) | YES  | UNI | NULL    |       |
| loc   | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值