查看表:
SHOW TABLES[FROM 数据库名][LIKE wild];
用“**SHOW CREATE TABLE 表名\G**”可以查看更全面的表定义信息
删除表:
DROP TABLE [IF EXISTS] 表名;
修改表的结构:
修改列类型
ALTER TABLE 表名 MODIFY 列名 列类型;
增加列
ALTER TABLE 表名 ADD 列名 列类型;
删除列
ALTER TABLE 表名 DROP 列名;
列改名
ALTER TABLE 表名 CHANGE 旧列名 新列名 列类型;
更改表名
ALTER TABLE 表名 RENAME 新表名;
RENAME TABLE 表名 TO 新表名;复制表的结构:
1、create table 新表名 like 源表
2、create table 新表名 select * from 源表(可拷贝源表记录)
3、如果已经存在一张机构一致的表,复制数据insert into 表 select * from 原表;
表中插入数据:
一次一条:INSERT INTO table [(column [, column...])] VALUES(value [, value...]);
一次多条:INSERT INTO table [(column [, column...])] VALUES(value [, value...]),(value [, value...])
修改表中数据:
UPDATE table SET column = value [, column = value] [WHERE condition];
mysql> create database if not exists db_hero default charset=utf8mb4;
Query OK, 1 row affected (0.01 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| db_hero |
| db_zpy |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)mysql> use db_hero
Database changed
我们先在数据库中创建一个名为t_intro的表,并且对它进行相关条件的约束,将它的id设置为主键,并且让它自增。name为英雄名称,nickname为绰号,gender为性别,age为年龄,story为英雄故事。
mysql> create table if not exists t_intro(
-> id int primary key auto_increment,
-> name varchar(50) not null unique,
-> nickname varchar(50) not null unique,
-> gender varchar(10) default "女",
-> age int check(age>=18) default 18,
-> story varchar(200) not null unique
-> )engine = innodb;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into t_intro values(1,"宋江","及时雨","男",34,"浔阳楼宋江吟反诗,三打祝家庄。");
mysql> insert into t_intro values(null,"卢俊义","玉麒麟","男",33,"卢俊义生擒史文恭。");
mysql> insert into t_intro values(null,"吴用","智多星","男",33,"吴用策划智取生辰纲。");
mysql> insert into t_intro values(null,"公孙胜","入云龙","男",35,"公孙胜大战高唐州破高廉八卦阵。");
mysql> insert into t_intro values(null,"关胜","大刀","男",36,"关胜降水火二将。");
mysql> insert into t_intro values(null,"秦明","霹雳火","男",40,"霹雳火夜走瓦砾场。");mysql> insert into t_intro values(null,"呼延灼","双鞭","男",33,"呼延灼计赚关胜。");
mysql> insert into t_intro values(null,"花荣","小李广","男",35,"花荣梁山射雁。");
mysql> insert into t_intro values(null,"小旋风","柴进","男",40,"柴进簪花入禁院。");
mysql> insert into t_intro values(null,"扑天雕","李应","男",36,"三打祝家庄。");
mysql> insert into t_intro values(null,"朱仝","美髯公","男",36,"朱仝义释晁盖、朱仝误失小衙内。");
mysql> insert into t_intro values(null,"鲁智深","花和尚","男",40,"鲁智深倒拔随杨柳、鲁智深大闹五台山。");
mysql> insert into t_intro values(null,"武松","行者","男",30,"武松打虎、武松杀嫂、醉打蒋门神、血溅鸳鸯楼。");
mysql> insert into t_intro values(null,"董平","双枪将","男",33,"大战东平府。");
mysql> insert into t_intro values(null,"张清","没羽箭","男",33,"大战东昌府。");
mysql> insert into t_intro values(null,"杨志","青面兽","男",33,"杨志卖刀。");
mysql> insert into t_intro values(null,"徐宁","金枪手","男",36,"钩镰枪大破连环马。");
mysql> insert into t_intro values(null,"索超","急先锋","男",34,"梁山大战大名府。");mysql> insert into t_intro values(null,"戴宗","神行太保","男",34,"梁山泊传假信、定计赚萧让。");
mysql> insert into t_intro values(null,"刘唐","赤发鬼","男",38,"智取生辰纲、七星聚义。");
mysql> insert into t_intro values(null,"李逵","黑旋风","男",35,"大闹江州,沂岭杀四虎、斧劈罗真人、怒砍杏黄旗。");
mysql> insert into t_intro values(null,"史进","九纹龙","男",26,"生擒跳涧虎陈达、大闹史家庄。");
mysql> insert into t_intro values(null,"穆弘","没遮拦","男",33,"白龙庙聚会、大闹江州。");
mysql> insert into t_intro values(null,"雷横","插翅虎","男",33,"雷横恶斗刘唐、雷横枷打白秀英。");
mysql> insert into t_intro values(null,"李俊","混江龙","男",35,"李俊三救宋江、白龙庙聚会。");
mysql> insert into t_intro values(null,"阮小二","立地太岁","男",34,"智取生辰纲、七星聚义、大破高俅水军。");
mysql> insert into t_intro values(null,"张横","船火儿","男",33,"大闹江州。");
mysql> insert into t_intro values(null,"阮小五","短命二郎","男",33,"取生辰纲、七星聚义,破高俅水军。");
mysql> insert into t_intro values(null,"张顺","浪里白条","男",33,"张顺活捉高俅。");mysql> insert into t_intro values(null,"杨雄","病关索","男",35,"杨雄杀妻、三打祝家庄。");
mysql> insert into t_intro values(null,"石秀","拼命三郎","男",36,"石秀捉奸、石秀独劫法场。");
mysql> insert into t_intro values(null,"解珍","双头蛇","女",35,"大闹登州府。");
mysql> insert into t_intro values(null,"解宝","双尾蝎","男",35,"闹登州府。");
mysql> insert into t_intro values(null,"燕青","浪子","男",30,"燕青打擂。");
mysql> insert into t_intro values(null,"朱武","神机军师","男",34,"大闹史家庄、朱武用计取大破昱岭关。");
mysql> insert into t_intro values(null,"黄信","镇三山","男",34,"大战清风山。");
mysql> insert into t_intro values(null,"孙立","病尉迟","男",34,"大闹登州府、三打祝家庄。");
mysql> insert into t_intro values(null,"宣赞","丑郡马","男",36,"降水火二将。");
mysql> insert into t_intro values(null,"韩滔","百胜将","男",36,"呼延灼连环马大战梁山。");
等等
mysql> alter table t_intro add nature varchar(100) default "豪爽";
mysql> alter table t_intro add ranking int default 108;
mysql> alter table t_intro drop ranking;
mysql> alter table t_intro rename to t_bawcock;