MySQL-資料庫操作

來源:互聯網
上載者:User

標籤:ima   char   create   alt   預設值   ide   建表   欄位   建立表   

1.建立表:

1 create table 表名(2     列名  類型  是否可以為空白,3     列名  類型  是否可以為空白4 )ENGINE=InnoDB DEFAULT CHARSET=utf8
建表
1         預設值,建立列時可以指定預設值,當插入資料時如果未主動設定,則自動添加預設值2             create table tb1(3                 nid int not null defalut 2,4                 num int not null5             )
預設值
 1        自增,如果為某列設定自增列,插入資料時無需設定此列,預設將自增(表中只能有一個自增列) 2             create table tb1( 3                 nid int not null auto_increment primary key, 4                 num int null 5             ) 6             或 7             create table tb1( 8                 nid int not null auto_increment, 9                 num int null,10                 index(nid)11             )12             注意:1、對於自增列,必須是索引(含主鍵)。13                  2、對於自增可以設定步長和起始值
自增
        主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。            create table tb1(                nid int not null auto_increment primary key,                num int null            )            或            create table tb1(                nid int not null,                num int not null,                primary key(nid,num)            )
主鍵
 1         外鍵,一個特殊的索引,只能是指定內容 2             creat table color( 3                 nid int not null primary key, 4                 name char(16) not null 5             ) 6  7             create table fruit( 8                 nid int not null primary key, 9                 smt char(32) null ,10                 color_id int not null,11                 constraint fk_cc foreign key (color_id) references color(nid)12             )
外鍵

2.刪除表

drop table 表名 --刪除表
刪表

3.清空表

delete from 表名 --清空所有資料truncate table 表名 --清空所有資料並重設表
清空

4.修改表

 1 添加列:alter table 表名 add 列名 類型 2 刪除列:alter table 表名 drop column 列名 3 修改列: 4         alter table 表名 modify column 列名 類型;  -- 類型 5         alter table 表名 change 原列名 新列名 類型; -- 列名,類型 6    7 添加主鍵: 8         alter table 表名 add primary key(列名); 9 刪除主鍵:10         alter table 表名 drop primary key;11         alter table 表名  modify  列名 int, drop primary key;12   13 添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);14 刪除外鍵:alter table 表名 drop foreign key 外鍵名稱15   16 修改預設值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;17 刪除預設值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
修改表6666

 

MySQL-資料庫操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.