Home » 插件 » MySQL笔记-数据表命令

MySQL笔记-数据表命令

编 辑:Y ┊ 时 间:2018年09月18日 ┊ 访问: 112 次
  1. 创建数据表
    create table 数据表名称(

    字段名1 类型(修饰符),
    字段名2 类型(修饰符),
    字段名n 类型(修饰符) #最后一行这里不用加逗号(,)

    );

demo:

create table news(
    id int primary key auto_increment,
    title varchar(50),
    author varchar(20),
    content text
);
  1. 查看数据库列表
    show tables;#最后加s

demo:

show tables;
  1. 查看数据库建表语句
    show create table news;

demo:

show create table news;
  1. 查看数据表结构
    desc 数据表名;

demo:

desc news;
还有第二种方法:describe 数据表名;
demo:

describe news;

  1. 删除数据表
    drop table 数据库表名称;

demo:

drop table news;
  1. 修改数据表(字符编码)
    alter table 数据表名称 选项;

demo:

alter table news charset=utf8;
  1. 数据表新增字段
    alter table 数据表名 add column 字段名 类型 修饰语句 位置;

demo:

alter table news add column newstime timestamp current_timestamp after content;
  1. 修改字段定义
    alter table 数据表名 modify column 字段名 新的定义;

demo:

alter table news modify column column content longtext;
  1. 修改字段名及定义
    alter table 数据表名 change column 旧字段名 新字段名 新的定义;

demo:

alter table news chages column title newstitle varchar(150);
  1. 删除字段

    alter table 数据表名 drop column 字段名;

demo:

alter table news drop column title;
  1. DISTINCT 关键词 (返回唯一不同值)
    语法: SELECT DISTINCT 列名称 FROM 表名称;

如果要从 "news" 列中选取所有的值,我们需要使用 SELECT 语句:

SELECT title FROM news;

如果title有新闻名称相同,则会出现两个相同的title
如:

  • WHY
  • hy
  • WHY
  • WQH

这里出现了title字段两个“WHY”,在结果集中,WHY 被列出了两次。
如需从 title" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句:
SELECT title Company FROM news




Copyright © 2026 Y 版权所有.网站运行:13年238天23小时16分