mysql表的清空、刪除和修改操作詳解
一、清除mysql表中數(shù)據(jù)
delete from 表名;
truncate table 表名;
不帶where參數(shù)的delete語句可以刪除mysql表中所有內(nèi)容,使用truncate table也可以清空mysql表中所有內(nèi)容。
效率上truncate比delete快,但truncate刪除后不記錄mysql日志,不可以恢復(fù)數(shù)據(jù)。
delete的效果有點(diǎn)像將mysql表中所有記錄一條一條刪除到刪完,
而truncate相當(dāng)于保留mysql表的結(jié)構(gòu),重新創(chuàng)建了這個表,所有的狀態(tài)都相當(dāng)于新表。
二、刪除表中的某些數(shù)據(jù)
delete from命令格式:delete from 表名 where 表達(dá)式
例如,刪除表 MyClass中編號為1 的記錄:
mysql> delete from MyClass where id=1;
三、修改表
1.選擇數(shù)據(jù)庫
>use 數(shù)據(jù)庫名;
2.查詢所有數(shù)據(jù)表
>show tables;
3.查詢表的字段信息
>desc 表名稱;
3.1.修改某個表的字段類型及指定為空或非空
>alter table 表名稱 change 字段名稱 字段名稱 字段類型 [是否允許非空];
>alter table 表名稱 modify 字段名稱 字段類型 [是否允許非空];
3.2.修改某個表的字段名稱及指定為空或非空
>alter table 表名稱 change 字段原名稱 字段新名稱 字段類型 [是否允許非空];
例如:
修改表expert_info中的字段birth,允許其為空
>alter table expert_info change birth birth varchar(20) null;
1.增加一個字段(一列)
alter table table_name add column column_name type default value; type指該字段的類型,value指該字段的默認(rèn)值
例如:
alter table mybook add column publish_house varchar(10) default ”;
2.更改一個字段名字(也可以改變類型和默認(rèn)值)
alter table table_name change sorce_col_name dest_col_name type default value; source_col_name指原來的字段名稱,dest_col_name
指改后的字段名稱
例如:
alter table Board_Info change IsMobile IsTelphone int(3) unsigned default 1;
3.改變一個字段的默認(rèn)值
alter table table_name alter column_name set default value;
例如:
alter table book alter flag set default '0′;
4.改變一個字段的數(shù)據(jù)類型
alter table table_name change column column_name column_name type;
例如:
alter table userinfo change column username username varchar(20);
5.向一個表中增加一個列做為主鍵
alter table table_name add column column_name type auto_increment PRIMARY KEY;
例如:
alter table book add column id int(10) auto_increment PRIMARY KEY;
6.數(shù)據(jù)庫某表的備份,在命令行中輸入:
mysqldump -u root -p database_name table_name > bak_file_name
例如:
mysqldump -u root -p f_info user_info > user_info.dat
7.導(dǎo)出數(shù)據(jù)
select_statment into outfile”dest_file”;
例如:
select cooperatecode,createtime from publish limit 10 into outfile”/home/mzc/temp/tempbad.txt”;
8.導(dǎo)入數(shù)據(jù)
load data infile”file_name” into table table_name;
例如:
load data infile”/home/mzc/temp/tempbad.txt” into table pad;
9.將兩個表里的數(shù)據(jù)拼接后插入到另一個表里。下面的例子說明將t1表中的com2和t2表中的com1字段的值拼接后插入到tx表對應(yīng)的
字段里。
例如:
insert into tx select t1.com1,concat(t1.com2,t2.com1) from t1,t2;
10,刪除字段
alter table form1 drop column 列名;
相關(guān)文章
lnmp重置mysql數(shù)據(jù)庫root密碼的兩種方法
這篇文章給大家介紹了lnmp重置mysql數(shù)據(jù)庫root密碼的兩種方法,第一種方法通過腳本重置密碼,第二種方法通過命令修改,具體操作方法大家參考下本文2017-07-07
EXCEL數(shù)據(jù)上傳到SQL SERVER中的簡單實(shí)現(xiàn)方法
以下是對EXCEL數(shù)據(jù)上傳到SQL SERVER中的簡單實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08
MySQL啟動報錯提示發(fā)生系統(tǒng)錯誤5,拒絕訪問的原因和解決方法
使用命令net start mysql啟動數(shù)據(jù)庫服務(wù)時候,出現(xiàn)如下錯誤提示發(fā)生系統(tǒng)錯誤5,拒絕訪問,所以本文給大家介紹了MySQL啟動報錯提示發(fā)生系統(tǒng)錯誤5,拒絕訪問的原因和解決方法,需要的朋友可以參考下2024-01-01
MySQL數(shù)據(jù)庫中表的查詢實(shí)例(單表和多表)
查詢數(shù)據(jù)是數(shù)據(jù)庫操作中最常用,也是最重要的操作,下面這篇文章主要介紹了MySQL數(shù)據(jù)庫中表的查詢的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03

