mysql資料庫的增刪改語句

來源:互聯網
上載者:User

標籤:let   alt   use   rom   插入   ...   set   select   約束   

#增加資料到xsb表

1.向表中插入資料

insert into xsb(xh,xm,nl,xb)

values(‘004‘,‘小王‘,18,‘男‘);

如果要插入表內所有欄位的資料,可以省略欄位名的書寫,但資料輸入必須與表中欄位順序一致.

例:

insert into xsb

valuse(‘005‘,‘小王‘,18,‘男‘);

2.如果想要同時插入多條資料,如下:

insert into xsb(xh,xm,xb,nl,jg,sfzh,zcrq)
values (‘029‘,‘小王‘,‘男‘,18,‘河北省邯鄲市‘,‘123456789123456729‘,‘2017-02-08 03:00:00‘),
(‘030‘,‘小王‘,‘男‘,18,‘河北省邯鄲市‘,‘123456789923456730‘,‘2017-02-08 03:00:00‘);

3.也可以使用select....union,例:

insert into xsb(xh,xm,xb,nl,jg,sfzh,zcrq)
select ‘033‘,‘小王‘,‘男‘,18,‘河北省邯鄲市‘,‘123456789123456733‘,‘2017-02-08 03:00:00‘
union
select ‘032‘,‘小王‘,‘男‘,18,‘河北省邯鄲市‘,‘123456789923456732‘,‘2017-02-08 03:00:00‘;

4.使用insert select 將a表中的資料添加到已有的b表中,注意,a表與b表格式類型要一致;

insert into b(xm,xb,nl)#表b的欄位名

select xm1,xb1,nl1   #表b的欄位名

from a

where xh=‘003‘ or xh=‘004‘;

#刪除資料從資料庫

1.格式

delete from where 條件

delete

from xsb 

where xm=‘小王‘;

2.如果要刪除表內所有資料,

delete from xsb;

truncate table xsb;

這樣刪除只會刪除表內的資料,不會破壞表的結構;

drop table xsb;

這樣會刪除整張表;

#更改表內資料

1.更改表內資料使用 update...set命令;

update xsb

set 更改的資料

where 條件

例:

update xsb

set nl=20,

    xb=‘女‘

where xm=‘張三‘;

2.如果要修改表內的欄位名 或者欄位名稱的約束類型,

#修改表名:

alter table xsb rename xsb2;

#修改欄位名稱

alter table xsb change 欄位名 新欄位名 類型;

#查看錶內結構

desc xsb;

#修改類型約束

alter table xsb change 欄位名 新欄位名 類型 約束;

#增加新欄位

alter table xsb add 欄位名;

#刪除欄位

alter table xsb drop column   欄位名;

 

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.