目录
例题:
1、创建一个英雄表(hero),管于四大名著的
主键
name 英雄(人物)名字
nickname 昵称 (代号)
address 那本书(西游,水浒)
groups 组(此时我们可以看为妖、神、鬼,国家等等)
email 邮件(随意编排)
telphone 电话(电话号码)
解题思路及步骤:
第一步:创建数据库 db_hero
第二步:创建表,包括约束的制定。 t_hero
第三步:插入数据
实验步骤:
步骤一:创建数据库
mysql> create database db_hero;
Query OK, 1 row affected (0.01 sec)
mysql> use db_hero
Database changedmysql> show tables;
Empty set (0.00 sec)
步骤二:创建表
mysql> CREATE TABLE IF NOT EXISTS t_hreo (
-> id int primary key auto_increment, # 此处经行主键约束并设置为自增长的整数类型
-> name varchar(50) not null unique, # 非空约束以及唯一约束
-> nickname varchar(255) unique, #
-> address varchar(20) check(address in("水浒", "西游")), # 8.0特有的默认值约束
-> `groups`varchar(20) not null, # 非空约束
-> email varchar(20),
-> telphone int
-> );
Query OK, 0 rows affected (0.03 sec)
代码:
CREATE TABLE IF NOT EXISTS t_hreo ( id int primary key auto_increment, name varchar(50) not null unique, nickname varchar(255) unique, address varchar(20) check(address in("水浒", "西游")), `groups`varchar(20) not null, email varchar(20), telphone int );
# 自我检查:
groups 为关机键字不能为列名,因此需要反引号
primary key 在同一个表中只能使用一次。
检查:
mysql> show tables;
+-------------------+ | Tables_in_db_hero | +-------------------+ | t_hreo | +-------------------+ 1 row in set (0.00 sec)
mysql> desc t_hreo;
+----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | UNI | NULL | | | nickname | varchar(255) | YES | UNI | NULL | | | address | varchar(20) | YES | | NULL | | | groups | varchar(20) | NO | | NULL | | | email | varchar(20) | YES | | NULL | | | telphone | int | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec)
步骤三:插入数据
由于此处要填入数据较多我们选择先写模板,当模板添加成功时。我们在经行批量话编辑,最后在统一输入。
经行输入模板:
mysql> insert into t_hreo values(null, "孙悟空", "齐天大圣" , "西游", " 妖" , "123@qq.123", 0123456789);
Query OK, 1 row affected (0.00 sec)mysql> select * from t_hreo;
+----+-----------+--------------+---------+--------+------------+-----------+ | id | name | nickname | address | groups | email | telphone | +----+-----------+--------------+---------+--------+------------+-----------+ | 1 | 孙悟空 | 齐天大圣 | 西游 | 妖 | 123@qq.123 | 123456789 | +----+-----------+--------------+---------+--------+------------+-----------+ 1 row in set (0.00 sec)
输出结果符合预期效果,开始进行统一编辑。
开始进行统一编辑:
西游—大唐F4
insert into t_hreo values(null, "孙悟空", "齐天大圣" , "西游", " 妖" , "123@qq.123", 0123456789); insert into t_hreo values(null, "唐三藏", "金蝉子" , "西游", " 人" , "123@qq.122", 0223456789); insert into t_hreo values(null, "猪八戒", "天蓬元帅" , "西游", " 妖" , "123@qq.121", 0323456789); insert into t_hreo values(null, "沙悟净", "卷帘大将" , "西游", " 妖" , "123@qq.124", 0423456789);
水浒—三十六天罡(部分)
insert into t_hreo values(null, "宋江", "呼保义" , "水浒", " 三十六天罡" , "123@qq.125", 0523456789); insert into t_hreo values(null, "卢俊义", "玉麒麟" , "西游", " 三十六天罡" , "123@qq.126", 0623456789); # 故意留个漏洞,懂得都懂 insert into t_hreo values(null, "吴用", "智多星" , "水浒", " 三十六天罡" , "123@qq.127", 0723456789); insert into t_hreo values(null, "公孙胜", "入云龙" , "水浒", " 三十六天罡" , "123@qq.128", 0823456789); insert into t_hreo values(null, "关胜", "大刀" , "水浒", " 三十六天罡" , "123@qq.129", 0923456789); insert into t_hreo values(null, "秦明", "霹雳火" , "水浒", " 三十六天罡" , "123@qq.110", 1023456789); insert into t_hreo values(null, "呼延灼", "双 鞭" , "水浒", " 三十六天罡" , "123@qq.111", 1123456789); insert into t_hreo values(null, "小李广", "花荣" , "水浒", " 三十六天罡" , "123@qq.112", 1223456789); insert into t_hreo values(null, "柴进", "小旋风" , "水浒", " 三十六天罡" , "123@qq.113", 1323456789); insert into t_hreo values(null, "李应", "扑天雕" , "水浒", " 三十六天罡" , "123@qq.114", 1423456789); insert into t_hreo values(null, "朱仝", "美髯公" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "鲁智深", "花和尚" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "董平", "双枪将" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "张清", "没羽箭" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "杨志", "青面兽" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "徐宁", "金枪手" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "索超", "急先锋" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "戴宗", "神行太保" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "李逵", "黑旋风" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "史进", "九纹龙" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "武松", "行者" , "水浒", " 三十六天罡" , "123@qq.115", 1523456789); insert into t_hreo values(null, "石秀", "拼命三郎", "水浒", " 三十六天罡" , "123@qq.115", 1523456789);
水浒—七十二地煞(部分)
insert into t_hreo values(null, "朱武", "神机军师", "水浒", " 七十二地煞" , "123@qq.116", 1623456789); insert into t_hreo values(null, "黄信", "镇三山", "水浒", " 七十二地煞" , "123@qq.117", 1723456789); insert into t_hreo values(null, "杨林", "锦豹子", "水浒", " 七十二地煞" , "123@qq.118", 1823456789); insert into t_hreo values(null, "燕顺", "锦毛虎", "水浒", " 七十二地煞" , "123@qq.119", 1923456789); insert into t_hreo values(null, "鲍旭", "丧门神", "水浒", " 七十二地煞" , "123@qq.120", 2023456789); insert into t_hreo values(null, "樊瑞", "混世魔王", "水浒", " 七十二地煞" , "123@qq.121", 2123456789); insert into t_hreo values(null, "杜迁", "摸着天", "水浒", " 七十二地煞" , "123@qq.122", 1111456789); insert into t_hreo values(null, "朱富", "笑面虎", "水浒", " 七十二地煞" , "123@qq.123", 1112456789); insert into t_hreo values(null, "时迁", "鼓上蚤", "水浒", " 七十二地煞" , "123@qq.124", 1113456789); insert into t_hreo values(null, "孙二娘", "母夜叉", "水浒", " 七十二地煞" , "123@qq.125", 1114456789);
注意:建议每执行一部分,就经行查表检测;此处我有检测,但未显示。
最后查表检测:
mysql> select * from t_hreo;
+----+-----------+--------------+---------+------------------+------------+------------+ | id | name | nickname | address | groups | email | telphone | +----+-----------+--------------+---------+------------------+------------+------------+ | 1 | 孙悟空 | 齐天大圣 | 西游 | 妖 | 123@qq.123 | 123456789 | | 2 | 唐三藏 | 金蝉子 | 西游 | 人 | 123@qq.122 | 223456789 | | 3 | 猪八戒 | 天蓬元帅 | 西游 | 妖 | 123@qq.121 | 323456789 | | 4 | 沙悟净 | 卷帘大将 | 西游 | 妖 | 123@qq.124 | 423456789 | | 5 | 宋江 | 呼保义 | 水浒 | 三十六天罡 | 123@qq.125 | 523456789 | | 6 | 卢俊义 | 玉麒麟 | 西游 | 三十六天罡 | 123@qq.126 | 623456789 | | 7 | 吴用 | 智多星 | 水浒 | 三十六天罡 | 123@qq.127 | 723456789 | | 8 | 公孙胜 | 入云龙 | 水浒 | 三十六天罡 | 123@qq.128 | 823456789 | | 9 | 关胜 | 大刀 | 水浒 | 三十六天罡 | 123@qq.129 | 923456789 | | 10 | 秦明 | 霹雳火 | 水浒 | 三十六天罡 | 123@qq.110 | 1023456789 | | 11 | 呼延灼 | 双 鞭 | 水浒 | 三十六天罡 | 123@qq.111 | 1123456789 | | 12 | 小李广 | 花荣 | 水浒 | 三十六天罡 | 123@qq.112 | 1223456789 | | 13 | 柴进 | 小旋风 | 水浒 | 三十六天罡 | 123@qq.113 | 1323456789 | | 14 | 李应 | 扑天雕 | 水浒 | 三十六天罡 | 123@qq.114 | 1423456789 | | 15 | 朱仝 | 美髯公 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 16 | 鲁智深 | 花和尚 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 18 | 董平 | 双枪将 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 19 | 张清 | 没羽箭 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 20 | 杨志 | 青面兽 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 21 | 徐宁 | 金枪手 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 22 | 索超 | 急先锋 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 23 | 戴宗 | 神行太保 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 24 | 李逵 | 黑旋风 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 25 | 史进 | 九纹龙 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 26 | 武松 | 行者 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 27 | 石秀 | 拼命三郎 | 水浒 | 三十六天罡 | 123@qq.115 | 1523456789 | | 28 | 朱武 | 神机军师 | 水浒 | 七十二地煞 | 123@qq.116 | 1623456789 | | 29 | 黄信 | 镇三山 | 水浒 | 七十二地煞 | 123@qq.117 | 1723456789 | | 30 | 杨林 | 锦豹子 | 水浒 | 七十二地煞 | 123@qq.118 | 1823456789 | | 31 | 燕顺 | 锦毛虎 | 水浒 | 七十二地煞 | 123@qq.119 | 1923456789 | | 32 | 鲍旭 | 丧门神 | 水浒 | 七十二地煞 | 123@qq.120 | 2023456789 | | 33 | 樊瑞 | 混世魔王 | 水浒 | 七十二地煞 | 123@qq.121 | 2123456789 | | 34 | 杜迁 | 摸着天 | 水浒 | 七十二地煞 | 123@qq.122 | 1111456789 | | 35 | 朱富 | 笑面虎 | 水浒 | 七十二地煞 | 123@qq.123 | 1112456789 | | 36 | 时迁 | 鼓上蚤 | 水浒 | 七十二地煞 | 123@qq.124 | 1113456789 | | 37 | 孙二娘 | 母夜叉 | 水浒 | 七十二地煞 | 123@qq.125 | 1114456789 | +----+-----------+--------------+---------+------------------+------------+------------+ 36 rows in set (0.01 sec)