要求:
1.在本机安装数据库
2.创建一个数据库db_classes
3.创建一行表db_hero
4.将四大名著中的常见人物插入这个英雄表
目录
要求:
过程:
结果:
命令总结:
过程:
1.安装数据库
http://t.csdnimg.cn/8TxVD
2.建数据库
mysql> create database db_classes
-> default charset = utf8mb4;
Query OK, 1 row affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db_classes |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
3.建表
mysql> use db_classes;
Database changed
mysql> show tables;
Empty set (0.01 sec)
mysql> create table db_hero(
-> id int not null,
-> name varchar(100) not null,
-> nickname varchar(100),
-> age int,
-> gender varchar(2),
-> address varchar(100),
-> weapon varchar(100),
-> world varchar(100),
-> types varchar(100)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc db_hero;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| nickname | varchar(100) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(2) | YES | | NULL | |
| address | varchar(100) | YES | | NULL | |
| weapon | varchar(100) | YES | | NULL | |
| world | varchar(100) | YES | | NULL | |
| types | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)
4.插入数据
mysql> insert into db_hero values(1,"孙悟空","齐天大圣",800,"男","花果山","如意金箍棒","西游记","妖族");
Query OK, 1 row affected (0.01 sec)
mysql> insert into db_hero values(2,"林黛玉","林妹妹",20,"女","贾府","倒拔垂杨柳","红楼梦","人类");
Query OK, 1 row affected (0.01 sec)
mysql> insert into db_hero values(3,"李逵","黑旋风",30,"男","沂州","两柄板斧","水浒传","人类");
Query OK, 1 row affected (0.00 sec)
mysql> insert into db_hero values(4,"赵云","常山赵子龙",27,"男","常山","涯角枪","三国演义","人类");
Query OK, 1 row affected (0.00 sec)
mysql> insert into db_hero values(5,"白龙马","小玉龙",3,"雄","东土大唐","","西游记","动物");
Query OK, 1 row affected (0.00 sec)
mysql> insert into db_hero values(6,"紫霞仙子","盘丝大仙",500,"女","盘丝洞","紫青宝剑","西游计","神仙");
Query OK, 1 row affected (0.00 sec)
结果:
mysql> show tables;
+----------------------+
| Tables_in_db_classes |
+----------------------+
| db_hero |
+----------------------+
1 row in set (0.00 sec)
mysql> select * from db_hero;
+----+--------------+-----------------+------+--------+--------------+-----------------+--------------+--------+
| id | name | nickname | age | gender | address | weapon | world | types |
+----+--------------+-----------------+------+--------+--------------+-----------------+--------------+--------+
| 1 | 孙悟空 | 齐天大圣 | 800 | 男 | 花果山 | 如意金箍棒 | 西游记 | 妖族 |
| 2 | 林黛玉 | 林妹妹 | 20 | 女 | 贾府 | 倒拔垂杨柳 | 红楼梦 | 人类 |
| 3 | 李逵 | 黑旋风 | 30 | 男 | 沂州 | 两柄板斧 | 水浒传 | 人类 |
| 4 | 赵云 | 常山赵子龙 | 27 | 男 | 常山 | 涯角枪 | 三国演义 | 人类 |
| 5 | 白龙马 | 小玉龙 | 3 | 雄 | 东土大唐 | | 西游记 | 动物 |
| 6 | 紫霞仙子 | 盘丝大仙 | 500 | 女 | 盘丝洞 | 紫青宝剑 | 西游计 | 神仙 |
+----+--------------+-----------------+------+--------+--------------+-----------------+--------------+--------+
6 rows in set (0.00 sec)
命令总结:
查看数据库 show databases;
创建数据库
create database 数据库名称 [default charset = utf8mb4]; 同时指定编码,否则参数默认
使用数据库 use 已存在的数据库名称;
删除数据库 drop database 数据库名称;
在某个数据库中执行表操作
查看所有表格 show tables;
创建表格 create table 表名称(
字段1名称 类型 [约束条件,]
字段2名称 类型 [约束条件,]
……
);查看表结构 describe 已存在的表;
插入数据
insert into 表名 values(xx, "xxx", xx, "xx");根据不同字段类型按顺序一一对应
查看表所有内容 select * from 表名;
删除表 drop table 表名;