mysql数据表的基本操做:表结构操做,字段操做

本节介绍:

 

表结构操做

  • 建立数据表、
  • 查看数据表和查看字段、
  • 修改数据表结构
  • 删除数据表

字段操做

  • 新增字段、
  • 修改字段数据类型、位置或属性、
  • 重命名字段
  • 删除字段

 

 

首发时间:2018-02-18  21:31mysql


表结构操做

 

建立数据表:

  • 语法  :

create table [if not exists] 表名( 字段名字 数据类型, 字段名字 数据类型 )[表选项];

 

  • 表选项  :

    • 字符集:charset表中存储数据的字符集
    • 校对集:colloate表中用来校对数据的校对集
    • 存储引擎  :engine存储数据的存储引擎
    • 表选项和库选项的区别是,若是不设置表选项就会采用库选项的设置,就好象一个“局部变量”。

 

  • 使用示例  :

-- 建表以前必须指定数据库,可使用use来指定后续的操做是基于哪一个数据库的 ,也可使用数据库名做为前缀来指定数据表建立在哪一个数据库。
-- 使用数据库名做为前缀来指定数据表建立在哪一个数据库。 create table
if not exists mydatabase.student( name varchar(20), sex varchar(20), number varchar(20), age int )charset utf8;
-- 使用use来指定后续操做基于哪一个数据库 use mydatabase; create table if not exists class( name varchar(20), room varchar(20) )charset utf8; -- 演示不带表选项的建立表 use mydatabase; create table if not exists class( name varchar(20), room varchar(20) );
  • 补充说明  :
    • if not exists 是先检查是否存在同名的表,若是存在,则不执行后面的建立语句。 十分建议使用。若是你肯定这个表不存在,那么能够不使用。
    • 若是没有指定表选项,将使用默认的,好比mysql默认的存储引擎是innodb。

 

 

 

查看数据表  :

查看数据表能够查看已有数据表、数据表的字段信息sql

  • 语法  :
-- 查看全部表
show tables; -- 查看部分表
show tables like '模糊匹配'; -- 查看表的建立语句
show create table 数据表名; -- 旋转查看结构
show create table 数据表名\G; -- 查看表结构:查看表中的字段信息:
Desc/desc 表名; describe 表名; show columns from 表名;

 

  • 模糊匹配
    • _匹配单个字符
    • %匹配多个字符

 

  • 使用示例  :
show tables;
show tables
like 'my%';
show
create table student;
show
create table student\G;
desc student; describe student; show columns from student;

图例:数据库

  1. show create table student;跟show create table sudent\G;

image

image

Desc/describe /show columns from 表名;url

 

image

 

 

 

 

修改数据表结构  :

修改表只能修改表名和表选项。spa

  • 语法  :
-- 修改表名:
rename table 老表名 to 新表名; --修改表选项:
Alter table 表名 表选项 [=] 值;
  • 使用示例  :
rename table student to my_student; rename table class to my_class; -- 
Alter table my_student charset gbk; Alter table my_collation_bin collate =utf8_bin;

 

 

 

 

 

删除数据表  :

  • 语法  :
Drop table 表名1,表名2...;
  • 使用示例  :
drop table demo; drop table demodata;
  • 补充说明  :
    • 删除不可恢复,删除需谨慎。


字段操做  :

 

 

新增字段  :

新增字段是在表存在的基础上新增字段.net

  • 语法  :
Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置];
  • 使用示例  :
Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置]; Alter table demo add column id int first; Alter table demo add id int; Alter table demo add  class int after age; Alter table demo add  number int not null after age;
  • 补充说明  :
    • 位置经常使用语法:
      • first :表示位于第一列,
      • after 字段名 :表明在某个字段后面;
    • 列属性:主键,空值 等;

 

 

修改字段  :

修改字段通常都是修改字段数据类型或者字段属性code

  • 语法  :
Alter table 表名 modify 字段名 数据类型 [属性] [位置];
  • 使用示例  :
Alter table my_student modify number char(10) after id; Alter table demo modify  number int null ; -- alter table student modify name varchar(20) not null; -- alter table student modify name varchar(20) not null primary key;
  • 补充说明  :
    • 字段名和数据类型是必填的,属性和位置是选填的。
    • 若是字段自己带着属性的,那么必须带上原来的,不然会被去除掉;若是须要在原有属性的基础上添加新的属性,则在填入时,那么在带上原有属性的基础上加上新的属性便可
    • image

 

 

 

 

重命名字段  :

  • 语法  :
Alter table 表名 change 旧字段 新字段 数据类型 [属性] [位置];
  • 使用示例  :
alter table demo change class room varchar(10); Alter table my_student change sex gender varchar(10);
  • 补充说明  :
    • 数据类型是必填的,但能够是新的【重名字段能够顺便改数据类型】
    • 更名的同时也能修改字段的数据类型,属性和位置。【若是字段带有属性,重命名字段时能够不带上】image

 

 

删除字段  :

  • 语法  :
Alter table 表名 drop 字段名;
  • 使用示例  :
Alter table my_student drop age; alter table demo drop room;
  • 补充说明  :
    • 删除需谨慎,删除字段表明着将该字段下的全部数据都将删除。
相关文章
相关标签/搜索