表介绍
表就相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段
还记得我们之前写过的‘员工信息表作业’么?存储这员工信息的文件是这样的:
id,name,age,sex,phone,job 1,Alex,83,female,13651054608,IT 2,Egon,26,male,13304320533,Tearcher 3,nezha,25,male,13332353222,IT 4,boss_jin,40,male,13332353333,IT
如果把上面这个文件改成一张表,应该是下面这个样子
id | name | age | sex | phone | job |
1 | Alex | 83 | female | 13651054608 | IT |
2 | Egon | 26 | male | 13304320533 | Teacher |
3 | nezha | 25 | male | 13332353222 | IT |
4 | boss_jin | 40 | male | 13332353333 | IT |
id,name,age,sex,phone,job称为字段,其余的,一行内容称为一条记录
创建表
#语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); #注意: 1. 在同一张表中,字段名是不能相同 2. 宽度和约束条件可选 3. 字段名和类型是必须的
mysql> create database staff;
Query OK, 1 row affected (0.00 sec)
mysql> use staff;
Database changed
mysql> create table staff_info (id int,name varchar(50),age int(3),sex enum('male','female'),phone bigint(11),job varchar(11));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-----------------+
| Tables_in_staff |
+-----------------+
| staff_info |
+-----------------+
1 row in set (0.00 sec)
mysql> desc staff_info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> select id,name,sex from staff_info;
Empty set (0.00 sec)
mysql> select * from staff_info;
Empty set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> use staff;
Database changed
mysql> create table staff_info (id int,name varchar(50),age int(3),sex enum('male','female'),phone bigint(11),job varchar(11));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-----------------+
| Tables_in_staff |
+-----------------+
| staff_info |
+-----------------+
1 row in set (0.00 sec)
mysql> desc staff_info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> select id,name,sex from staff_info;
Empty set (0.00 sec)
mysql> select * from staff_info;
Empty set (0.00 sec)
mysql> insert into staff_info (id,name,age,sex,phone,job) values (1,'Alex',83,'female',13651054608,'IT');
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff_info values (2,'Egon',26,'male',13304320533,'Teacher');
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff_info values (3,'nezha',25,'male',13332353222,'IT'),(4,'boss_jin',40,'male',13332353333,'IT');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from staff_info;
+------+----------+------+--------+-------------+---------+
| id | name | age | sex | phone | job |
+------+----------+------+--------+-------------+---------+
| 1 | Alex | 83 | female | 13651054608 | IT |
| 2 | Egon | 26 | male | 13304320533 | Teacher |
| 3 | nezha | 25 | male | 13332353222 | IT |
| 4 | boss_jin | 40 | male | 13332353333 | IT |
+------+----------+------+--------+-------------+---------+
4 rows in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff_info values (2,'Egon',26,'male',13304320533,'Teacher');
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff_info values (3,'nezha',25,'male',13332353222,'IT'),(4,'boss_jin',40,'male',13332353333,'IT');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from staff_info;
+------+----------+------+--------+-------------+---------+
| id | name | age | sex | phone | job |
+------+----------+------+--------+-------------+---------+
| 1 | Alex | 83 | female | 13651054608 | IT |
| 2 | Egon | 26 | male | 13304320533 | Teacher |
| 3 | nezha | 25 | male | 13332353222 | IT |
| 4 | boss_jin | 40 | male | 13332353333 | IT |
+------+----------+------+--------+-------------+---------+
4 rows in set (0.00 sec)
查看表结构
查看表结构有两种方式:
describe [tablename];这种方法和desc [tablename];效果相同;可以查看当前的表结构
虽然desc命令可以查看表的定义,但是其输出的信息还不够全面,为了得到更全面的表定义信息,有时候就需要查看创建表的SQL语句,使用show create table语法。除了可以看到表定义之外,还可以看到engine(存储引擎)和charset(字符集)等信息。(G选项的含义是是的记录能够竖向排列,以便更好的显示内容较长的记录。)
mysql> describe staff_info; +-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec) mysql> desc staff_info; +-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec) mysql> show create table staff_infoG; *************************** 1. row ***************************
Table: staff_info Create Table: CREATE TABLE `staff_info` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `age` int(3) DEFAULT NULL, `sex` enum('male','female') DEFAULT NULL, `phone` bigint(11) DEFAULT NULL, `job` varchar(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.01 sec) ERROR: No query specified
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec) mysql> desc staff_info; +-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec) mysql> show create table staff_infoG; *************************** 1. row ***************************
Table: staff_info Create Table: CREATE TABLE `staff_info` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `age` int(3) DEFAULT NULL, `sex` enum('male','female') DEFAULT NULL, `phone` bigint(11) DEFAULT NULL, `job` varchar(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.01 sec) ERROR: No query specified