zoukankan      html  css  js  c++  java
  • SQL入门表定义

    1.DDL管理数据库-表定义

    表名 列定义 列名称  属性 数据类型 约束 默认值 

    1.1创建表

    create table anyux.test (id int);

    创建多个列

    create table anyux.t1(
    idcard int ,
    name char(30),
    sex char(10)
    );

     1.2 create table 语句

    创建表
        CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
            (create_definition,...)
            [table_options]
            [partition_options]
    创建列
        create_definition:
            col_name column_definition
          | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
              [index_option] ...
          | {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
              [index_option] ...
          | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
              [index_name] [index_type] (index_col_name,...)
              [index_option] ...
          | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
              [index_option] ...
          | [CONSTRAINT [symbol]] FOREIGN KEY
              [index_name] (index_col_name,...) reference_definition
          | CHECK (expr)
    列定义
        column_definition:
            data_type [NOT NULL | NULL] [DEFAULT default_value]
              [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
              [COMMENT 'string']
              [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
              [STORAGE {DISK|MEMORY|DEFAULT}]
              [reference_definition]
    View Code

    1.3 查看表定义

    desc anyux.t1;
    
    

    2.修改表定义

    2.1修改表名

    rename table anyux.t1 to anyux.test1;
    alter table anyux.test1 rename to anyux.t1;

    2.2修改表结构

    2.2.1添加新的列

    --添加新的列
    alter table anyux.t1 add addr char(40) NOT NULL;
    --在所有列最后创建列
    alter table anyux.t1 add age int NOT NULL after sex; 
    --在指定列之后创建列
    alter table anyux.t1 add pid int(10) NOT NULL first;
    --将列放在最前面
    --创建多列
    alter table anyux.t1 add ( id int , comment char(40) );

     2.2.2添加删除列

    --删除列
    alter table anyux.t1 drop pid;
    --修改列结构
    alter table anyux.t1 modify name char(20);
    --修改列名称
    alter table anyux.t1 change name pepolename char(30);
    --也可以移动出来
    alter table anyux.t1 change pepolename name char(30) after name ;
    
    
  • 相关阅读:
    第四章:Django模型——添加 Event发布会的表 报错
    第四章:Django模型——admin后台管理
    第四章:Django 模型 —— 设计系统表
    第三章:3.9 清除 Google 浏览器中的缓存
    第三章:3.9 关上窗户
    第三章:3.9 引用Django 认证登陆
    第三章:3.8 登陆 Django 默认后台
    降脂食物
    决定孩子人生高度的,不是知识而是这个!
    百万保险
  • 原文地址:https://www.cnblogs.com/anyux/p/8119275.html
Copyright © 2011-2022 走看看