zoukankan      html  css  js  c++  java
  • mysql 关于表与字段的增删改查操作

    1、mysql 查看 所有的数据表

    格式: show tables;                          # 一定要在use 数据库之后才可以查看数据库

    2、mysql 查看数据表结构

    格式: desc 表名;

    desc member;

    3、mysql 查看表的创建结构

    格式:show create table 表名

    show create table t1;

    4、mysql删除数据表

    格式: drop table 数据表;

    drop table member;

    5、mysql创建数据表

    格式:CREATE TABLE IF NOT EXISTS `表名` (

        `字段名` 数据类型 [unsigned(无符号)|zerofill(填充0)] [NOT NULL | NULL] [ DEFAULT NULL | AUTO_INCREMENT] [COMMENT 注释],

        PRIMARY KEY(`主键`),

        KEY `索引名`(栏位) 

       )ENGINE=搜索引擎 DEFAULT CHARSET=编码(utf8) COMMENT='表注释'

    注解:[unsigned|zerofill] 只适用于数字类型。

    CREATE TABLE IF NOT EXISTS `student` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生id',
      `name` varchar(30) DEFAULT NULL,
      `age` int(3) NULL DEFAULT '12' COMMENT '学生年龄',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT '学生表';

    6、修改mysql表名

    格式: ALTER TABLE 表名 RENAME TO 新表名

    ALTER TABLE `student_test` RENAME TO `student`;

    7、复制旧表结构创建新表

    格式: CREATE TABLE 新表名 LIKE 旧表名

    create table tp_notification_queue like tp_notification_queue_201803

    8、修改表字段数据结构

    格式: ALTER TABLE 数据表 MODIFY COLUMN 字段名 数据类型 [NOT NULL | NULL] [DEFAULT NULL] [COMMENT '注释']  [AFTER 字段]

    注解: [after] 表示显示在哪个字段后面

    alter table `student` modify column `age` int(4) NULL DEFAULT '15' COMMENT '年龄' AFTER `id`;

    9、添加表字段

    格式: ALTER TABLE 表名 ADD 字段名 数据类型 [NOT NULL | NULL] [DEFAULT NULL] [COMMENT '注释']  [AFTER 字段]

    alter table `student_test` add `sex` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1男0女' after `name`;

    10、修改表字段

    格式:ALTER TABLE 表名 CHANGE 原字段名 现字段名 数据类型 [NOT NULL | NULL] [DEFAULT NULL] [COMMENT '注释']  [AFTER 字段]

    alter table `student_test` change `sex` `sex_test` tinyint(1) not null default '1' comment '1男0女' after `age`;

    11、删除表字段

    格式: ALTER TABLE 表名 DROP COLUMN 字段名;

     alter table student_test drop column sex, drop column age;
  • 相关阅读:
    LeetCode 453 Minimum Moves to Equal Array Elements
    LeetCode 112 Path Sum
    LeetCode 437 Path Sum III
    LeetCode 263 Ugly Number
    Solutions and Summay for Linked List Naive and Easy Questions
    AWS–Sysops notes
    Linked List
    All About Linked List
    datatable fix error–Invalid JSON response
    [转]反编译c#的相关问题
  • 原文地址:https://www.cnblogs.com/xiaobaiskill/p/8583130.html
Copyright © 2011-2022 走看看