MySQL資料庫基本操作(二)

來源:互聯網
上載者:User

標籤:after   mysql資料庫   aaa   alter   沒有   char   重用   incr   uniq   

表結構操作 ( ALTER TABLE)
添加單列:ALTER TABLE tb1_name ADD [COLUNM] col_namecolumn_definition [FIRST|AFTER col-name]

  

create table `tb1`(    `id` int,      `name` varchar(20));例:mysql> ALTER TABLE `tb1`    -> ADD `age` INT    -> ;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> ALTER TABLE `tb1`    -> ADD `number` INT FIRST    -> ;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> ALTER TABLE `tb1` ADD `aaa` INT after `id`;
添加多列:ALTER TABLE tbl_name ADD [COLUMN](col_name column_definition,...)

  

例:mysql> ALTER TABLE `tb1`    -> add  `aa` INT,    -> add  `bb` INT,    -> add  `cc` INT    -> ;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0mysql>
刪除資料表中的列ALTER TABLE tbl_name DROP [COLUMN] col_name ;

  

#例:mysql> ALTER TABLE `tb1`    -> DROP `aa`    -> ;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> ALTER TABLE `tb1`    -> DROP `bb`,    -> DROP `cc`    -> ;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0
表結構操作的補充:
ALTER TABLE `tbname`增加 :ADD刪除 :DROP 修改 :MODIFY  #改列的資料類型 (屬性)  CHANGE  #改列名和資料類型  RENAME  #改表名修改列名mysql> alter table tb1 change `name` `sex` varchar(20);修改資料類型mysql> alter table tb1 modify `age` varchar(20);修改表名mysql> ALTER TABLE `tb1` RENAME TO `students`;Query OK, 0 rows affected (0.40 sec)
非空約束

NULL 欄位值可以為空白

NOT NULL 欄位值不可為空

例:mysql> CREATE TABLE tb1(     -> id INT,    -> name VARCHAR(20) NOT NULL    -> );Query OK, 0 rows affected (0.01 sec)# 當有非空約束,指定插入,必須加上name。mysql> insert into tb1(id) value(1);   # 報錯mysql> insert into tb1(id,name) value(1,‘佳能‘);Query OK, 1 row affected (0.01 sec)## 注意   在mysql 裡面,‘‘ 不等於null#手動,添加非空約束 (必須這個欄位,沒有NULL值)mysql> alter table tb1    -> modify id int not null;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0# 取消非空約束mysql> alter table tb1    -> modify id int ;
唯一約束

確保欄位中的值的唯一unique key

 

例:mysql> create table tb2(    -> id int not null unique key,      -> name varchar(20) not null    -> );    mysql> insert into tb2 value(1,‘張三‘);Query OK, 1 row affected (0.00 sec)mysql> insert into tb2 value(1,‘張三‘);  # 報錯,違反唯一約束    #添加唯一約束mysql> ALTER TABLE `tb2`    -> ADD unique key(`name`)    -> ;#刪除唯一約束mysql> desc tb2;mysql> alter table tb2    -> drop key name;#聯合唯一mysql> alter table tb2    -> add aa int,    -> add bb int;mysql> alter table tb2    -> add unique key (aa,bb);mysql> insert into tb2 value(4,‘佳能‘,1,2);mysql> insert into tb2 value(5,‘哈哈‘,1,2);ERROR 1062 (23000): Duplicate entry ‘1-2‘ for key ‘aa‘# 刪除聯合唯一  (show create table tb2;)alter table tb2 drop key aa;
主鍵約束

主鍵保證記錄的唯一性, 唯一標識每一條資料主鍵自動為NOT NULL每張資料表只能存在一個主鍵NOT NULL + UNIQUE KEY

一個UNIQUE KEY 又是一個NOT NULL的時候,那麼它被當做PRIMARY KEY主鍵當一張表裡沒有一個主鍵的時候,第一個出現的非空且為唯一的列被視為有主鍵。

#主鍵,就是可以資料表中,可以唯一標識,一條資料。就好像身份證一樣。mysql> create table tb3(     -> id int primary key,    -> name varchar(20) not null    -> );  mysql> desc tb3;mysql> insert into tb3 value(1,‘張三‘);Query OK, 1 row affected (0.27 sec)mysql> insert into tb3 value(1,‘張三‘);ERROR 1062 (23000): Duplicate entry ‘1‘ for key ‘PRIMARY‘#刪除主鍵約束mysql> alter table tb3    -> drop primary key;    # 一個表裡面,只有一個主鍵mysql> desc tb3;#添加主鍵約束mysql> alter table tb3    -> add primary key(id);    #聯合主鍵mysql> create table tb4(    -> id_a int ,    -> id_b int,    -> content varchar(20),    -> primary key(id_a,id_b)    -> );mysql> desc tb4;#刪除主鍵約束mysql> alter table tb4    -> drop primary key;#添加聯合主鍵mysql> alter table tb4    -> add primary key(id_a,id_b);
自增長 AUTO_INCREMENT

AUTO_INCREMENT 自動編號,一般與主鍵組合使用。一個表裡面只有一個自增預設情況下,起始值為1,每次的增量為1。當插入記錄時,如果為AUTO_INCREMENT資料列明確指定了一個數值,則會出現兩種情況,

情況一,如果插入的值與已有的編號重複,則會出現出錯資訊,因為AUTO_INCREMENT資料列的值必須是唯一的;

情況二,如果插入的值大於已編號的值,則會把該插入到資料列中,並使在下一個編號將從這個新值開始遞增。也就是說,可以跳過一些編號。如果自增序列的最大值被刪除了,則在插入新記錄時,該值被重用。(可以調大,不可以縮小)

例:mysql> create table tb5(     -> id int primary key auto_increment,    -> name varchar(20)    -> )auto_increment =100;    # 如果不寫,預設從1開始mysql> desc tb5;mysql> insert into tb5(name) values(‘張三‘),(‘李四‘);mysql> select * from tb5;#auto_increment值,可以調大insert into tb5(id,name) values(110,‘王五‘);mysql> select * from tb5;#不可以調小insert into tb5(id,name) values(108,‘王八‘);insert into tb5(name) values(‘田七‘);mysql> select * from tb5;#刪除自動成長mysql> alter table tb5    -> modify id int;#增加自動成長auto_incrementmysql> alter table tb5    -> modify id int auto_increment;
預設約束 DEFAULT

DEFAULT(預設約束)初始值設定,插入記錄時,如果沒有明確為欄位賦值,則自動賦予預設值。

添加/刪除預設約束ALTER TABLE tbl_name ALTER [COLUMN] col_name{SET DEFAULT literal | DROP DEFAULT}
#例:mysql> create table tb6(    -> id int primary key auto_increment,    -> name varchar(20) not null,    -> age int not null default 18    -> );mysql> desc tb6;mysql> insert into tb6(name) values(‘張三‘),(‘李四‘),(‘王五‘);mysql> select * from tb6;#刪除defaultmysql> alter table tb6    -> modify age int not null;mysql> desc tb6;#(2)mysql> alter table tb6     -> alter age drop default;#添加defaultmysql> alter table tb6     -> modify age int default 20;mysql> desc tb6;#(2)mysql> alter table tb6    -> alter age set default 21;
作業

創捷一張 學生表(
學號 主鍵
名字 不為空白 唯一
性別
年齡 不為空白

1. 刪除名字的唯一
2. 添加性別,不為空白、預設為男
3. 設定年齡 預設為18

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.