创建表
语法:
create table [if not exists] `表名`(
'字段名1' 列类型 [属性][索引][注释],
'字段名2' 列类型 [属性][索引][注释],
......
'字段名n' 列类型 [属性][索引][注释]
)[表类型][表字符集][注释];
在SQLyog中操作:
1.创建一个数据库
2.直接创建表
创建成功后,在‘信息’栏可以看到创建该表的代码(SQLyog会自己生成)
在历史信息栏,也可以看到创建该表的代码
3.通过代码创建表:
将代码写在【询问】里
刷新后得到【another】表(一定要刷新):
修改表
连接数据库:
【之后不再显示命令行截图,直接会拷贝到代码块里,看起来清楚。】如下:
C:\Users\鹿爷>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
//显示所有数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| haha |
| mysql |
| performance_schema |
| school |
| smbms |
| sys |
+--------------------+
7 rows in set (0.04 sec)
mysql>
修改表名:
ALTER TABLE 旧表名 RENAME [TO] <新表名>;
//注意命令以【;】结尾
如下,将数据库【haha】中的表【another】的名字改为【msg】:
mysql> use haha;
Database changed
mysql> show tables;
+----------------+
| Tables_in_haha |
+----------------+
| another |
| info |
+----------------+
2 rows in set (0.04 sec)
mysql> ALTER TABLE ANOTHER
-> RENAME TO MSG;
Query OK, 0 rows affected (0.29 sec)
mysql> show tables;
+----------------+
| Tables_in_haha |
+----------------+
| info |
| msg |
+----------------+
2 rows in set (0.00 sec)
添加字段:
//一个完整的字段包括字段名、数据类型、完整性约束
//FIRST作用是将新添加的字段设置为表的第一个字段;AFTER 作用是将新添加的字段添加到指定的已存在的字段名的后面,它们为可选参数。
ALTER TABLE 表名 ADD 新字段名 数据类型 [约束条件] [FIRST|AFTER 已存在的字段名];
如下,在表【msg】添加一个字段
mysql> alter table msg
-> add phone char first;
Query OK, 0 rows affected (0.86 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc msg
-> ;
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| phone | char(1) | YES | | NULL | |
| id | bigint(20) | NO | PRI | NULL | |
| hobby | char(30) | YES | | NULL | |
| address | char(50) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.05 sec)
修改字段数据类型
ALTER TABLE 表名 MODIFY 字段名 数据类型;
如下,修改表【msg】phone字段的类型为int
mysql> alter table msg
-> modify phone int;
Query OK, 0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc msg;
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| phone | int(11) | YES | | NULL | |
| id | bigint(20) | NO | PRI | NULL | |
| hobby | char(30) | YES | | NULL | |
| address | char(50) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.05 sec)
修改字段名称
//新数据类型不能为空,如果不想改就直接写之前的
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型;
如下,修改表【info】的id字段名为iidd
mysql> alter table info
-> change id iidd int;
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc info;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| iidd | int(11) | NO | PRI | NULL | |
| name | char(30) | YES | | NULL | |
| sex | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
删除字段
ALTER TABLE 表名 DROP 字段名;
如下,删除表【msg】的phone字段
mysql> alter table msg
-> drop phone;
Query OK, 0 rows affected (0.68 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc msg;
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| id | bigint(20) | NO | PRI | NULL | |
| hobby | char(30) | YES | | NULL | |
| address | char(50) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
删除表
DROP TABLE [IF EXISTS] 表名