MySQL數(shù)據(jù)庫(kù)應(yīng)用實(shí)戰(zhàn)教程(慕課版)(第2版)實(shí)訓(xùn)指導(dǎo)-3-5 字段操作_第1頁(yè)
MySQL數(shù)據(jù)庫(kù)應(yīng)用實(shí)戰(zhàn)教程(慕課版)(第2版)實(shí)訓(xùn)指導(dǎo)-3-5 字段操作_第2頁(yè)
MySQL數(shù)據(jù)庫(kù)應(yīng)用實(shí)戰(zhàn)教程(慕課版)(第2版)實(shí)訓(xùn)指導(dǎo)-3-5 字段操作_第3頁(yè)
MySQL數(shù)據(jù)庫(kù)應(yīng)用實(shí)戰(zhàn)教程(慕課版)(第2版)實(shí)訓(xùn)指導(dǎo)-3-5 字段操作_第4頁(yè)
MySQL數(shù)據(jù)庫(kù)應(yīng)用實(shí)戰(zhàn)教程(慕課版)(第2版)實(shí)訓(xùn)指導(dǎo)-3-5 字段操作_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

實(shí)訓(xùn)案例名稱(chēng):字段操作一、任務(wù)介紹針對(duì)數(shù)據(jù)庫(kù)表的字段,可以對(duì)字段進(jìn)行設(shè)置為主鍵、設(shè)置為復(fù)合主鍵、添加字段、改變字段類(lèi)型、字段重命名、字段設(shè)置默認(rèn)值以及設(shè)置自增字段的操作。二、實(shí)現(xiàn)步驟1.主鍵設(shè)置#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#創(chuàng)建學(xué)生表studentmysql>CREATETABLEstudent(idintnotnull,namevarchar(255)notnull,nointnotnull,PRIMARYKEY(id))ENGINE=InnoDBDEFAULTCHARSET=utf8;QueryOK,0rowsaffected(0.60sec)#查看表主鍵mysql>SHOWCREATETABLEstudent;+---------+----------------------------------------------------------+|Table|CreateTable+---------+----------------------------------------------------------+|student|CREATETABLE`student`(`id`int(11)NOTNULL,`name`varchar(255)NOTNULL,`no`int(11)NOTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8|+---------+----------------------------------------------------------+1rowinset(0.09sec)#刪除表主鍵mysql>ALTERTABLEstudentDROPprimarykey;QueryOK,0rowsaffected(0.96sec)Records:0Duplicates:0Warnings:0#學(xué)生表student沒(méi)有主鍵mysql>SHOWCREATETABLEstudent;+---------+-----------------------------------------------------------+|Table

|CreateTable+---------+-----------------------------------------------------------+|student|CREATETABLE`student`(`id`int(11)NOTNULL,`name`varchar(255)NOTNULL,`no`int(11)NOTNULL)ENGINE=InnoDBDEFAULTCHARSET=utf8|+---------+----------------------------------------------------------+1rowinset(0.06sec)#設(shè)置no為主鍵mysql>ALTERTABLEstudentaddprimarykey(no);QueryOK,0rowsaffected(0.78sec)Records:0Duplicates:0Warnings:0#學(xué)生表student有主鍵mysql>SHOWCREATETABLEstudent;+---------+----------------------------------------------------------+|Table

|CreateTable+---------+----------------------------------------------------------+|student|CREATETABLE`student`(`id`int(11)NOTNULL,`name`varchar(255)NOTNULL,`no`int(11)NOTNULL,PRIMARYKEY(`no`))ENGINE=InnoDBDEFAULTCHARSET=utf8|+---------+----------------------------------------------------------+1rowinset(0.00sec)mysql>2.設(shè)置為復(fù)合主鍵#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#創(chuàng)建人員表person,設(shè)置復(fù)合主鍵id,namemysql>CREATETABLEperson(idintnotnull,namevarchar(255)notnull,jobvarchar(255)notnull,PRIMARYKEY(id,name))ENGINE=InnoDBDEFAULTCHARSET=utf8;QueryOK,0rowsaffected(0.63sec)#查看人員表personmysql>SHOWCREATETABLEperson;+--------+-----------------------------------------------------+|Table|CreateTable+--------+-----------------------------------------------------+|person|CREATETABLE`person`(`id`int(11)NOTNULL,`name`varchar(255)NOTNULL,`job`varchar(255)NOTNULL,PRIMARYKEY(`id`,`name`))ENGINE=InnoDBDEFAULTCHARSET=utf8|+--------+-----------------------------------------------------+1rowinset(0.05sec)mysql>3.添加/刪除字段#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#向用戶(hù)表user添加字段phonemysql>ALTERTABLEuseraddphonevarchar(25)notNull;QueryOK,0rowsaffected(0.98sec)Records:0Duplicates:0Warnings:0#查看用戶(hù)表user的表結(jié)構(gòu)mysql>DESCuser;+----------+--------------+------+-----+---------+----------------+|Field

|Type

|Null

|Key

|Default|Extra |+----------+--------------+------+-----+---------+----------------+|id

|intunsigned

|NO

|PRI

|NULL

|auto_increment ||name

|varchar(25)

|YES

|

|NULL

| ||sex

|varchar(5)

|YES

|

|NULL

| ||age

|int

|YES

|

|NULL

| ||password

|varchar(25)

|YES

|

|NULL

| ||phone

|varchar(25)

|NO

|

|NULL

| |+----------+--------------+------+-----+---------+----------------+6rowsinset(0.00sec)#刪除用戶(hù)表user中的phone字段mysql>ALTERTABLEuserDROPphone;QueryOK,0rowsaffected(0.02sec)Records:0Duplicates:0Warnings:04.改變字段類(lèi)型#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#修改用戶(hù)表user,將phone字段的varchar類(lèi)型修改為char類(lèi)型mysql>ALTERTABLEuserMODIFYphonechar(11)notNull;QueryOK,9rowsaffected(0.05sec)Records:9Duplicates:0Warnings:0#查看用戶(hù)表user的表結(jié)構(gòu)mysql>DESCuser;+----------+--------------+------+-----+---------+----------------+|Field

|Type

|Null

|Key|Default|Extra |+----------+--------------+------+-----+---------+----------------+|id

|intunsigned

|NO

|PRI|NULL|auto_increment ||name

|varchar(25)

|YES

||NULL| ||sex

|varchar(5)

|YES

||NULL| ||age

|int

|YES

||NULL| ||password

|varchar(25)

|YES

||NULL| ||phone

|char(11)

|NO

||NULL| |+----------+--------------+------+-----+---------+----------------+6rowsinset(0.00sec)5.字段重命名#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#查看用戶(hù)表user的表結(jié)構(gòu)mysql>DESCuser;+----------+--------------+------+-----+---------+----------------+|Field

|Type

|Null

|Key|Default

|Extra |+----------+--------------+------+-----+---------+----------------+|id

|intunsigned

|NO

|PRI|NULL

|auto_increment ||name

|varchar(25)

|YES

||NULL

| ||sex

|varchar(5)

|YES

||NULL

| ||age

|int

|YES

||NULL

| ||password

|varchar(25)

|YES

||NULL

| ||phone

|char(11)

|NO

||NULL

| |+----------+--------------+------+-----+---------+----------------+6rowsinset(0.02sec)#將用戶(hù)表user中的phone字段重命名為telephone字段mysql>ALTERTABLEuserchangephonetelephonechar(11);QueryOK,0rowsaffected(0.07sec)Records:0Duplicates:0Warnings:0#查看用戶(hù)表user的表結(jié)構(gòu)mysql>DESCuser;+-----------+--------------+------+-----+---------+----------------+|Field

|Type

|Null

|Key

|Default|Extra

|+-----------+--------------+------+-----+---------+----------------+|id

|intunsigned

|NO

|PRI

|NULL|auto_increment

||name

|varchar(25)

|YES

|

|NULL|

||sex

|varchar(5)

|YES

|

|NULL|

||age

|int

|YES

|

|NULL|

||password

|varchar(25)

|YES

|

|NULL|

||telephone

|char(11)

|YES

|

|NULL|

|+-----------+--------------+------+-----+---------+----------------+6rowsinset(0.00sec)mysql>6.字段設(shè)置默認(rèn)值#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#創(chuàng)建部門(mén)表deptmysql>CREATETABLEdept(idintnotnull,deptNamevarchar(255)notnulldefault'軟件事業(yè)部',userNamevarchar(25)notnull,PRIMARYKEY(id))ENGINE=InnoDBdefaultcharset=utf8;QueryOK,0rowsaffected(0.41sec)#插入數(shù)據(jù),但不插入deptName字段的值mysql>INSERTINTOdept(id,userName)VALUES(1,'kevin');QueryOK,1rowaffected(0.02sec)#查詢(xún)部門(mén)表dept,可發(fā)現(xiàn)雖然沒(méi)有插入deptName字段的值,但是會(huì)插入默認(rèn)值mysql>SELECT*FROMdept;+----+-----------+----------+|id|deptName

|userName|+----+-----------+----------+|1|軟件事業(yè)部

|kevin|+----+-----------+----------+1rowinset(0.00sec)#修改deptName字段的默認(rèn)值mysql>ALTERTABLEdeptALTERdeptNameSETdefault'辦公室';QueryOK,0rowsaffected(0.10sec)Records:0Duplicates:0Warnings:0#插入數(shù)據(jù),但不插入deptName字段的值mysql>INSERTINTOdept(id,userName)VALUES(2,'tom');QueryOK,1rowaffected(0.00sec)#查詢(xún)部門(mén)表dept,可發(fā)現(xiàn)雖然沒(méi)有插入deptName字段的值,但是會(huì)插入新的默認(rèn)值mysql>SELECT*FROMdept;+----+------------+----------+|id

|deptName

|userName

|+----+------------+----------+|1

|軟件事業(yè)部

|kevin

||2

|辦公室

|tom

|+----+------------+----------+2rowsinset(0.01sec)#刪除deptName的默認(rèn)值mysql>ALTERTABLEdeptALTERdeptNameDROPdefault;QueryOK,0rowsaffected(0.12sec)Records:0Duplicates:0Warnings:0#插入數(shù)據(jù)時(shí),如果沒(méi)有默認(rèn)值,則不允許插入mysql>INSERTINTOdept(id,userName)VALUES(3,'david');ERROR1364(HY000):Field'deptName'doesn'thaveadefaultvalue#插入數(shù)據(jù)mysql>INSERTINTOdept(id,deptName,userName)VALUES(3,'人力資源部','david');QueryOK,1rowaffected(0.00sec)#查詢(xún)數(shù)據(jù)mysql>SELECT*FROMdept;+----+------------+----------+|id|deptName

|userName

|+----+------------+----------+|1|軟件事業(yè)部

|kevi

||2|辦公室

|tom

||3|人力資源部

|david

|+----+------------+----------+3rowsinset(0.00sec)mysql>7.設(shè)置自增字段#使用用戶(hù)名root和相應(yīng)密碼,連接本地MySQLC:\Users\Administrator>mysql-uroot-p123456mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.#使用shop數(shù)據(jù)庫(kù)mysql>USEshop;Databasechanged#創(chuàng)建t_zizeng表,設(shè)置id字段為自增字段,但是沒(méi)有將其設(shè)置為主鍵,MySQL會(huì)報(bào)錯(cuò)mysql>CREATETABLEt_zizeng(idintauto_increment,namevarchar(255));ERROR1075(42000):Incorrecttabledefinition;therecanbeonlyoneautocolumnanditmustbedefinedasakey#創(chuàng)建t_zizeng表,將id字段設(shè)置為自增字段,并把它設(shè)置為主鍵mQueryOK,0rowsaffected(0.32sec)#插入一條數(shù)據(jù),自增字段的值為null,它會(huì)自動(dòng)生成mysql>INSERTINTOt_zizeng(id,name)VALUES(null,'小明');QueryOK,1rowaffected(0.00sec)#查詢(xún)t_zizeng表,可以看到id字段的值自動(dòng)生成為1mysql>SELECT*FROMt_zizeng;+----+------+|id|name

|+----+------+|1|小明

|+----+------+1rowinset(0.00sec)#插入一條數(shù)據(jù),自增字段可以不用賦值,MySQL會(huì)自動(dòng)增加mysql>INSERTINTOt_zizeng(name)VALUE

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論