MySQL第三课
首先创建一个数据库:
CREATE DATABASE ku;
Query OK, 1 row affected
查看一下是否有此数据库:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ku |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set
使用此数据库:
USE ku;
Database changed
创建数据表:
CREATE TABLE biao(
name VARCHAR(20),
age INT(11),
sex CHAR(11),
love VARCHAR(11)
);
Query OK, 0 rows affected
查看表结构:
DESC biao;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(11) | YES | | NULL | |
| love | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set
修改表名:
ALTER TABLE biao RENAME biao1;
Query OK, 0 rows affected
修改字段和类型:
ALTER TABLE biao1 CHANGE SEX SEX1 CHAR(20);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
用圆括号!!!!
再查看一边:
DESC biao1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| SEX1 | char(20) | YES | | NULL | |
| love | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set
修改字段的排列位置:
ALTER TABLE biao1 MODIFY NAME VARCHAR(20) AFTER SEX1;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看:
DESC biao1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| age | int(11) | YES | | NULL | |
| SEX1 | char(20) | YES | | NULL | |
| NAME | varchar(20) | YES | | NULL | |
| love | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set
注意改的是大写出现的也是大写
加一个think字段:
ALTER TABLE biao1 ADD think VARCHAR (10) AFTER LOVE;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看:
DESC biao1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| age | int(11) | YES | | NULL | |
| SEX1 | char(20) | YES | | NULL | |
| NAME | varchar(20) | YES | | NULL | |
| love | varchar(11) | YES | | NULL | |
| think | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
5 rows in set
删除think:
ALTER TABLE biao1 DROP think;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看:
DESC biao1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| age | int(11) | YES | | NULL | |
| SEX1 | char(20) | YES | | NULL | |
| NAME | varchar(20) | YES | | NULL | |
| love | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set
删除此表:
DROP TABLE biao1;
Query OK, 0 rows affected
查看:
SHOW TABLES;
Empty set
删除数据库:
DROP DATABASE ku;
Query OK, 0 rows affected
查看:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341