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 ;

 

 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM