复制表的结构;只能复制表结构,不能复制表数据
语法二:create table 新表名 like 需要复制的表名; mysql> create table stu2 like stu; Query OK, 0 rows affected mysql> select * from stu2; Empty set mysql> desc stu2; #主键被复制了 +-------+-------------+------+-----+----------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+----------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | addr | varchar(50) | YES | | 地址不详 | | | score | int(11) | YES | | NULL | | +-------+-------------+------+-----+----------+----------------+ 4 rows in set 显示创建表的语句 语法:show create table 表名; show create table test; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set 查看表结构 语法:desc 表名; #缩写 describe 表名; describe test; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set 修改表编码(解决中文编码问题)
alter table 表名character set gbk; alter table 表名 convert to character set utf8; set names gbk; #最简单的方法,修改整个数据库最后面的都是编码名称,按需要填写,一般常用的也就 gbk/utf8/gb2312
数据操作创建测试表
create table stu( -> id int auto_increment primary key comment\'主键\', -> name varchar(20) not null, -> addr varchar(50) default\'地址不详\', -> score int comment\'成绩\' -> ); Query OK, 0 rows affected 插入一条数据 语法:insert into 表名(字段名,字段名,…)values(字段值,…,字段值);例一:
insert into stu (id,name,addr,score) values(19022100,\'tom\',\'北京\',88); Query OK, 1 row affected例二:插入的字段可以和表的字段顺序不一致只要一一对应即可
insert into stu(name,score,addr,id) values(\'berry\',77,\'北京\',2); Query OK, 1 row affected例三:可以只插入部分字段,但是非空字段必须插入
insert into stu(id,name,addr) values(19,\'ketty\',\'上海\'); Query OK, 1 row affected例四:自动增长列的值可以不用也可以直接插入null,数据库会自动插入增长的数
insert into stu(name,addr) values(\'rose\',\'北京\'); Query OK, 1 row affected例五:插入值的顺序和个数与表字段的顺序和个数一致,插入的字段可以省略
insert into stu values(null,\'大海\',\'广西\',100); Query OK, 1 row affected例七:通过default关键字插入默认值
insert into stu values(null,\'toni\',default,50); Query OK, 1 row affected 查询表里的所有数据 语法:select * from 表名; select * from stu; +----------+------+------+-------+ | id | name | addr | score | +----------+------+------+-------+ | 19022100 | tom | 北京 | 88 | +----------+------+------+-------+ 1 row in set 插入多条数据 语法:insert into 表名 values(字段值1,...,字段值n),....,(字段值1,...字段值n); insert into stu values(null,\'马云\',\'杭州\',66),(null,\'马化腾\',\'深圳\',55); Query OK, 2 rows affected 更新数据 语法:update 表名 set 字段值=值 [where 条件];例一:把2号学生的地址改为湖北
update stu set addr = \'湖北\' where id = 2; Query OK, 1 row affected例二:把马云的成绩改为99
update stu set score = 99 where name = \'马云\'; Query OK, 1 row affected例三:将马化腾的地址改为广东,成绩改为80
update stu set addr = \'广东\',score = 80 where name = \'马化腾\'; Query OK, 1 row affected例四:将北京的学生成绩全部改为77
update stu set score = 60 where addr = \'北京\'; Query OK, 2 rows affected例五:将学号为1,2的两位学生成绩改为65
update stu set score = 65 where id = 1 or id = 2; Query OK, 2 rows affected条件可以省略,但是如果省略就是更改所有数据
删除数据 语法:delete from 表名 [where 条件];例一:删除学号是2的学生
delete from stu shere id = 2;例二:删除小于等于65分的学生
delete from stu where score <= 65; Query OK, 4 rows affected