数据库创建表并插入数据练习题

一、创建表的要求

创建一个英雄表(hero)
主键
name
nickname
address
groups
email
telphone

二、 操作步骤

1.登录MySQL

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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           |
+--------------------+
| db_han             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

2.创建数据库和表

注意:关键词建议用大写

mysql> create database db_hero;
Query OK, 1 row affected (0.00 sec)

mysql> use db_hero;        #这句可以不跟分号
Database changed
mysql> create table if not exists t_hero (
    -> id int primary key auto_increment,
    -> name varchar(50) not null unique,
    -> nickname varchar(255),
    -> address varchar(50),
    -> `groups` varchar(25),        #注意:groups是关键字,则需用反引号引起或稍做改变这个单词
    -> email varchar(50),
    -> telphone varchar(11) unique
    -> );
Query OK, 0 rows affected (0.02 sec)

数据库创建表并插入数据练习题_第1张图片

3. 插入数据

mysql> insert into t_hero values (null,"宋江","及时雨",null,"水浒传","[email protected]","15112346666");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"吴用","智多星",null,"水浒传","[email protected]","15122331144");
Query OK, 1 row affected (0.01 sec)

mysql> insert into t_hero values (null,"林冲","豹子头",null,"水浒传","[email protected]","15111223456");
Query OK, 1 row affected (0.01 sec)

mysql> insert into t_hero values (null,"鲁智深","花和尚",null,"水浒传","[email protected]","15199884567");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"武松","行者",null,"水浒传","[email protected]","15145663245");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"李逵","黑旋风",null,"水浒传","[email protected]","15145456767");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"刘备","玄德","河北涿州","三国演义","[email protected]","15298675544");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"沙僧","沙和尚","天宫卷帘大将","西游记","[email protected]","15544338877");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"关羽","美髯公","山西运城","三国演义","[email protected]","15233556633");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"张飞","翼德",null,"三国演义","[email protected]","15244331234");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"猪八戒","天蓬元帅","天宫将领","西游记","[email protected]","15545673221");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"赵云","常胜将军",null,"三国演义","[email protected]","15212344321");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"诸葛亮","卧龙","山东临沂市沂南县","三国演义","[email protected]","15299776543");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"孙悟空","齐天大圣","花果山","西游记","[email protected]","15532423432");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"贾宝玉","宝二爷","荣国府","红楼梦","[email protected]","17732467632");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"唐僧","玄奘","大唐","西游记","[email protected]","15534543678");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_hero values (null,"林黛玉","潇湘妃子","荣国府外戚","红楼梦","[email protected]","17744557654");
Query OK, 1 row affected (0.00 sec)

4.查看表内容

数据库创建表并插入数据练习题_第2张图片

你可能感兴趣的:(数据库,linux,mysql,MySQL,运维)