MySQL-2.表的基本操作

2. 表的基本操作

2.1 表的概念

  • 数据库——厂库,表——货架(对数据进行抽象分类)。

  • 在创建数据库的时候一定要记得设置字符编码。

2.2 引用数据库-use

mysql> use student;  // 切换到目标数据库student 
Database changed   // 反馈信息,表示已经成功切换

2.3 查询当前数据库中的所有表-show

mysql> show tables;
Empty set (0.00 sec)   // 反馈信息,表示查询结果为空,即当前数据库中没有表

2.4 创建表-create

2.4.1 简单

mysql> create table employee(
    -> id int,
    -> name varchar(30),
    -> age int,
    -> salary int
    -> );
Query OK, 0 rows affected (0.04 sec)
MySQL中字符串的语句不使用String,而是使用varchar,需要表明长度。 
mysql> show tables;
+-----------------------+
| Tables_in_student |
+-----------------------+
| employee              |
+-----------------------+

 2.4.2 企业中

关键词解释:

  • auto_increment: 对字段进行自动增长
  • primary key: 主键,是关系型数据库连接桥梁,必须填写不能空着
  • comment: 注释
  • not null: 不能为空,必须填写
  • default: 如果为空那么表中这个数据默认为'  '中的内容(要和所定义的数据类型一样)
  • engine = innodb: 表示数据库引擎
mysql> create table staff(
    -> id int auto_increment primary key comment'主键id',
    -> name varchar(30) not null,
    -> age int comment'年龄',
    -> salary int default '0' comment'薪水'
    ->  )engine=innodb;
Query OK, 0 rows affected (0.03 sec)
mysql> create table if not exists teacher(
    -> id int auto_increment primary key comment'主键id',
    -> name varchar(30) not null comment '老师的名字',
    -> phone varchar(20) comment '电话号码',
    -> address varchar(100) default '暂时未知' comment '住址'
    -> )engine=innodb;
Query OK, 0 rows affected (0.03 sec)

2.5 查看表 

2.5.1 显示表-show

show create table employee;
mysql> show create table empolyee;
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| empolyee | CREATE TABLE `empolyee` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(30) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `salary` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> show create table staff;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| staff | CREATE TABLE `staff` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(30) NOT NULL,
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `salary` int(11) DEFAULT '0' COMMENT '薪水',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 2.5.2 查看表的结构-desc

 desc teacher;
mysql> desc teacher;
+---------+--------------+------+-----+----------+----------------+
| Field   | Type         | Null | Key | Default  | Extra          |
+---------+--------------+------+-----+----------+----------------+
| id      | int(11)      | NO   | PRI | NULL     | auto_increment |
| name    | varchar(30)  | NO   |     | NULL     |                |
| phone   | varchar(20)  | YES  |     | NULL     |                |
| address | varchar(100) | YES  |     | 暂时未知 |                |
+---------+--------------+------+-----+----------+----------------+
4 rows in set (0.01 sec)
  • NULL :代表是否能为空

  • Key PRI :代表唯一值

  • Default :是否设立default

  • Extra :代表的是规则,上述文件是自动增加

2.6 删除表-drop

drop table if exists staff;   // 删一个
drop table if exists staff,empolyee;  // 删多个

2.7 修改表-alter

2.7.1 添加字段-add

alter table 表名 add 字段名 字段类型;
mysql> alter table student add name varchar(30);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
  •  指定在某字段后添加-after
alter table 表名 add 字段名 字段类型 after 字段名;

mysql> alter table student add age int after id;
  •  在第一行添加-first
alter table 表名 add 字段 字段类型 first;

mysql> alter table student add phone int(20) first;

 2.7.2 删除字段-drop

alter table 表名 drop 字段;

mysql> alter table student drop phone;

2.7.3 修改字段

  • 修改字段名和类型(完全修改)-change
alter table 表名 change 字段名 新的字段名 字段类型;

mysql> alter table student change age phone varchar(20);
  • 修改类型-modify
alter table 表名 modify 字段名 要改成的字段类型;

mysql> alter table student modify phone int(20);

 2.7.4 修改表名-rename to

alter table 表名 rename to 新表名;

mysql> alter table student rename to empolyee;

参考:http://t.csdnimg.cn/VQ89m