MySQL——数据库之表的操作

1. 表的创建

// mysql语义优先
// 列 类型
// 表名也可以不带''
create table [if not exists] 'tablename'(
columnname1 datatype,
columnname2 datatype
) [character set 字符集 collate 校验规则 engine 存储引擎];

一般使用charset=utf8 engine=INnoDB
最后的三项内容不写的话则使用设定的默认值

2. 表的查看

2.1 查看库中所有表

 show tables;
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| ok               |
| tchar            |
| tcomment         |
| tdefault         |
| test             |
| test2            |
+------------------+

2.2 查看表的结构

desc tablename;
mysql> desc ok;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| a     | char(10) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

2.3 查看表的创建语句

show create table tablename;
// \G去掉特殊提示
mysql> show create table ok\G
*************************** 1. row ***************************
       Table: ok
Create Table: CREATE TABLE `ok` (
  `a` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

3. 表的修改

// 增加列
// after:可以指定插在某一列后,但是不支持before 
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...)[after columnname];

// 修改列信息
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);

// 删除列
alter table tablename drop (column);

// 修改表名
alter table oldname rename [to] newname;

// 修改列名
// 新字段需要完整定义字段名和数据类型等信息
alter table tablename change oldname newname datatype;

4. 表的删除

删除表的全部数据内容

drop table [if exists] tablename[,tablename2];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值