使用MySQL创建表格(实例)

用sql语句生成以下表格:
在这里插入图片描述

mysql> create database book;
Query OK, 1 row affected (0.01 sec)

mysql> use book;
Database changed
mysql> create table reader(
    -> card_id char(18),
    -> name varchar(10),
    -> sex enum('男','女'),
    -> age tinyint,
    -> tel char(11),
    -> balance decimal(7,3)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc reader;
+---------+-------------------+------+-----+---------+-------+
| Field   | Type              | Null | Key | Default | Extra |
+---------+-------------------+------+-----+---------+-------+
| card_id | char(18)          | YES  |     | NULL    |       |
| name    | varchar(10)       | YES  |     | NULL    |       |
| sex     | enum('男','女')   | YES  |     | NULL    |       |
| age     | tinyint           | YES  |     | NULL    |       |
| tel     | char(11)          | YES  |     | NULL    |       |
| balance | decimal(7,3)      | YES  |     | NULL    |       |
+---------+-------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> show tables from book;
+----------------+
| Tables_in_book |
+----------------+
| reader         |
+----------------+
1 row in set (0.00 sec)


四种查看数据库表的方式:
a. show tables [from <database_name>];
b. show colums from <table_name>;
c. desc/describe <table_name>;
d. show create table <table_name>;

1.在表中插入第一条数据:
命令:insert into <table_name> values();

mysql> insert into reader values
    -> ('610111199901011000','Sara','female',22,'19991149827',300);
Query OK, 1 row affected (0.01 sec)

mysql> select * from reader;
+--------------------+------+--------+------+-------------+---------+
| card_id            | name | sex    | age  | tel         | balance |
+--------------------+------+--------+------+-------------+---------+
| 610111199901011000 | Sara | female |   22 | 19991149827 | 300.000 |
+--------------------+------+--------+------+-------------+---------+
1 rows in set (0.00 sec)
  1. 在表中同时插入两条数据
mysql> insert into reader values
    -> ('610111199801011001','John','male',23,'13861578234',300),
    -> ('610111199701011002','Tony','male',24,'13617823041',300);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> insert into reader values
    -> ('610111199601011003','Lily','female',25,'18992318099',300),
    -> ('610111199501011004','Raza','male',26,'13116289309',300);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from reader;
+--------------------+------+--------+------+-------------+---------+
| card_id            | name | sex    | age  | tel         | balance |
+--------------------+------+--------+------+-------------+---------+
| 610111199901011000 | Sara | female |   22 | 19991149827 | 300.000 |
| 610111199801011001 | John | male   |   23 | 13861578234 | 300.000 |
| 610111199701011002 | Tony | male   |   24 | 13617823041 | 300.000 |
| 610111199601011003 | Lily | female |   25 | 18992318099 | 300.000 |
| 610111199501011004 | Raza | male   |   26 | 13116289309 | 300.000 |
+--------------------+------+--------+------+-------------+---------+
5 rows in set (0.00 sec)
  1. 给表中的部分item插入数据
    命令: insert into <table_name> <column_name1, [column_name2, …]> values( );
mysql> insert into reader(name, sex) values 
    -> ('Frank','male'),
    -> ('Tom','male');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from reader;
+--------------------+-------+--------+------+-------------+---------+
| card_id            | name  | sex    | age  | tel         | balance |
+--------------------+-------+--------+------+-------------+---------+
| 610111199901011000 | Sara  | female |   22 | 19991149827 | 300.000 |
| 610111199801011001 | John  | male   |   23 | 13861578234 | 300.000 |
| 610111199701011002 | Tony  | male   |   24 | 13617823041 | 300.000 |
| 610111199601011003 | Lily  | female |   25 | 18992318099 | 300.000 |
| 610111199501011004 | Raza  | male   |   26 | 13116289309 | 300.000 |
| NULL               | Frank | male   | NULL | NULL        |    NULL |
| NULL               | Tom   | male   | NULL | NULL        |    NULL |
+--------------------+-------+--------+------+-------------+---------+
7 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值