MySQL增刪改查操作

2019-01-04 11:57 更新

增刪改查操作

查詢表中的所有的記錄:select from 表名(xs)

創(chuàng)建數(shù)據(jù)庫:create database if not exists xsgl;

8.2創(chuàng)建表:cerate table if not exists(判斷是否存在) 表名(xsb)

8.3刪除:drop database if exists 數(shù)據(jù)庫名 (xsgl)

向表中插入記錄:

insert into 表名(xsl) values(‘081101’,’王琳’,’計算機’,’女’,’1990-2-10’,50,null,null);

insert into xsl(學(xué)號,姓名,總學(xué)分)values(‘王燕’,50);

insert into xsl set 學(xué)號=’081104’,姓名=’韋言平’,性別=’男’,出生日期=’1989-3-12’;

注意:(必須在打開數(shù)據(jù)庫的情況下才能創(chuàng)建表和插入記錄)創(chuàng)建表:

創(chuàng)建表:

create table if not exists 表名(學(xué)號 char(6) primary key not null,姓名 char(4),專業(yè) varchar(100),性別 char(1),出生日期 date,總學(xué)分 decimal(4.1),照片 blob,備注 text);

例:創(chuàng)建成績表

Create table if not exists cjb(學(xué)號 char(6) not null,課程號 char(3) not null,成績 decimal(4.1),Primary key(學(xué)號,課程號));

復(fù)制表

A. 復(fù)制表的結(jié)構(gòu):create table xs2(復(fù)制后生成的表名) Like xs1 (被復(fù)制表名);

B. 復(fù)制表中的數(shù)據(jù):Create table xs3(復(fù)制后生成的表名) as select from xs1(被復(fù)制表名);

修改表的結(jié)構(gòu)

添加字段:alter table xs2 add 家庭住址 varchar(100) after(指定放在哪個字段后面) 總學(xué)分;

//向xs2表中添加字段“家庭住址”。

刪除字段:Alter table xs2 drop 家庭住址;

將xs2表中的家庭住址字段刪除。

添加主鍵:Alter table xs3 add primary key(學(xué)號);

//在xs3表的學(xué)號字段上添加一個主鍵。

刪除主鍵:Alter table xs3 drop primary key;

//刪除xs3表中的主鍵;

注意:一個表中只有一個主鍵。

添加默認值:Alter table xs3 alter 專業(yè) set default ‘汽車維修’;

//為專業(yè)字段設(shè)置一個默認值為“汽車維修”。

6. 刪除默認值:Alter table xs3 alter 專業(yè) drop default;

//刪除xs3表中專業(yè)字段的默認值。

7.修改字段的類型、字符集:Alter table xs3 modify 姓名 varchar(100) character set utf8;

//將xs3表中的姓名字段類型改為varchar(100),字符集改為utf8。

8.修改字段的名稱、類型:Alter table xs3 change 專業(yè) 專業(yè)名 varchar(100);

//將專業(yè)字段改名為專業(yè)名。

9.查看表的信息:Show create table xs3;

//查看xs3表的信息。

10.查看MySQL數(shù)據(jù)庫中默認的存儲引擎:Show engines;

11.修改表的存儲引擎:Alter table kc(表名) engine=myisam(存儲引擎);//將kc表存儲引擎改為myisam。

12.查看mysql服務(wù)器支持的字符集:Show character set;

13.修改表的字符集:Alter table xs3 default charset=utf8;

//將xs3表的字符集改為utf8。

修改表中的數(shù)據(jù)

1、 將xs3表中的學(xué)號為081101的姓名改為張杰:

Update(刷新) xs3 set 姓名=’張杰’ where(那里) 學(xué)號=’081101’;

如要修改多個則用英文逗號隔開。

2、 刪除表:Drop table xs3;//刪除xs3表。

3、 將kc2表中的學(xué)分小于5分的每條記入加0.5分:Update kc2 set 學(xué)分=學(xué)分+0.5 where 學(xué)分=85;

4.在xsl表中查詢出計算機專業(yè)的男生學(xué)號,姓名,專業(yè)和性別Select 學(xué)號,姓名,專業(yè),性別 from xsl where 專業(yè)=’計算機’ and 性別=’男’;

注意:兩個條件要同時滿足,使用and(而且),表示邏輯與運算

在xsl表中查詢出學(xué)號為081101和081106的兩條記錄Select from xsl where 學(xué)號=’081101’ or 學(xué)號=’081106’;

注意:兩個條件只要滿足其中的一個就可以了,使用or(或者),表示邏輯或運算在xsl表中查詢出非通信工程專業(yè)的學(xué)生的記錄

Select from xsl where 專業(yè)’通信工程’;

Select from xsl where 專業(yè)!=’通信工程’;

Select from xsl where not 專業(yè)=’通信工程’;

注意:邏輯非運算,not表示當(dāng)前條件之外的。表示多個或運算時用in。

例:select from xs where 學(xué)號 in(‘081101’,’081105’,’081108’ );

在xsb中查詢出學(xué)號不是081101、081103和081107的記錄

Select from xs where 學(xué)號 not in(‘081101’,’0881102’,’081103’);

Select from xs where not 專業(yè)=’計算機’;

注意:邏輯非運算,not in表示不包含,如果是單個條件就在where后面加上not。

9.使用between??????and表示兩個數(shù)值之間或兩個日期之間的與運算的條件查詢,Not between ??????and 表示不在莫兩者之間的條件查詢;

例:在成績表中查詢成績在60到85之間的記錄

Select from cj where 成績>=60 and 成績=’1989-1-1’ and 出生日期=75 order by 2 desc;

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號