找文章 / 找答案

[SQL独家资料] MYSQL技术大全-(8)MySQL操作数据表

曾梦闵皇冠三 显示全部楼层 发表于 5 天前 |阅读模式 打印 上一主题 下一主题
【摘选】MYSQL技术大全-冰河编著(MySQL操作数据表)
在MySQL中,数据表以二维表格的形式展示,表格中的一行代表一条完整的数据记录,表格中的一列代表数据的某个特定属性。


8.1.1 创建空数据表
create table [if not exists] 表名(
字段1  数据类型  [约束条件] [默认值],
字段2  数据类型  [约束条件] [默认值],
...
[表约束条件]
);
注:Windows不区分大小写,Linux区分大小写。Linux修改MySQL配置文件my.cnf添加:lower_case_table_names = 1 ,则不区分大小写。


8.1.2 创建数据表时指定主键
主键又称主码,包含表中的一列或多列,能够唯一标识表中的一行记录。
语法:
# 定义列指定主键
字段 数据类型 primary key [默认值]
# 定义表指定主键
[constraint 约束条件名] primary key [字段1,字段2...,字段n]


8.1.3 创建数据表时指定外键
在一张表中定义了外键之后不允许删除另一张表中具有关联关系的行数据。
[constraint 外键名] foreign key 字段1 [,字段2...]  references 主表名 主键列1 [,主键列2...]


8.1.4 创建数据表时指定字段非空
字段名称 数据类型  not null


8.1.5 创建数据表时指定默认值
字段名称 数据类型 default 默认值


8.1.6 创建数据表时指定主键默认递增
字段名称 数据类型 auto_increment


8.1.7 创建数据表时指定存储引擎
创建表语句后:engine = 存储引擎名称


8.1.8 创建数据表时指定编码
default character set 编码 collate 校对规则

default charset=编码 collate=校对规则


8.2.1 查看表结构
describe 表名称

desc 表名称


8.2.2 查看表结构
show create table 表名 \G


8.3.1 修改数据表名称
alter table 原表名 rename [to] 新表名


8.3.2 添加字段
alter table 表名 add columu 新字段名 数据类型 [not null default 默认值]


8.3.3 添加字段时指定位置
# 第一列
alter table 表名 add columu 新字段名 数据类型 [not null default 默认值] first
# 指定字段后添加字段
alter table 表名 add columu 新字段名 数据类型 [not null default 默认值] after 原有字段名


8.3.4 修改字段名称
alter table 表名 change 原有字段名 新字段名 新数据类型


8.3.5 修改字段数据类型
alter table 表名 modify 字段名 新数据类型 [default 默认值]


8.3.6 修改字段位置
alter table 表名 modify 字段名 数据类型 first
alter table 表名 modify 字段名 数据类型 after 字段2名称


8.3.7 删除字段
alter table 表名 drop 字段名


8.4.1 删除没有关联关系的数据表
drop table [if exists] 数据表1 [,数据表2 ...]


8.4.2 删除有外键约束的主表
1- 先删除有外键约束的从表,再删除主表。
2- 先解除外键约束,再删除主表。


8.5.1 创建临时表
create temporary table [if not exists] 表名


8.5.2 删除临时表
同删除普通表
drop table [if exists] 表名
回复

使用道具 举报

高级模式
您需要登录后才可以回帖 登录 | 免费注册

  • 官方微信

    欢迎关注永洪服务号!收费为0,价值无限

    扫码关注
  • 新浪微博

    让每位用户轻松挖掘数据价值!

    访问新浪微博
  • 智能客服
50W

会员等你来哦

Copyright   ©2012-2024  北京永洪商智科技有限公司  (京ICP备12050607) 京公网安备110110802011451号 |联系社区管理员|《永洪社区协议》
返回顶部