1.登錄數(shù)據(jù)庫(kù)
>mysql -h localhost -u root -p 數(shù)據(jù)庫(kù)名稱(chēng)
2.查詢(xún)所有的數(shù)據(jù)庫(kù)
>show databases;
3.選擇數(shù)據(jù)庫(kù)
>use 數(shù)據(jù)庫(kù)名;
4.查詢(xún)所有數(shù)據(jù)表
>show tables;
5.查詢(xún)表的字段信息
>desc 表名稱(chēng);
6.1.修改某個(gè)表的字段類(lèi)型及指定為空或非空
>alter table 表名稱(chēng) change 字段名稱(chēng) 字段名稱(chēng) 字段類(lèi)型 [是否允許非空];
>alter table 表名稱(chēng) modify 字段名稱(chēng) 字段類(lèi)型 [是否允許非空];
6.2.修改某個(gè)表的字段名稱(chēng)及指定為空或非空
>alter table 表名稱(chēng) change 字段原名稱(chēng) 字段新名稱(chēng) 字段類(lèi)型 [是否允許非空];
例如:
修改表expert_info中的字段birth,允許其為空
>alter table expert_info change birth birth varchar(20) null;
1.增加一個(gè)字段(一列)
alter table table_name add column column_name type default value;?? type指該字段的類(lèi)型,value指該字段的默認(rèn)值
例如:alter table mybook add column publish_house varchar(10) default '';
2.更改一個(gè)字段名字(也可以改變類(lèi)型和默認(rèn)值)
alter table table_name change sorce_col_name dest_col_name type default value;?? source_col_name指原來(lái)的字段名稱(chēng),dest_col_name
指改后的字段名稱(chēng)
例如:alter table Board_Info change IsMobile IsTelphone int(3) unsigned default 1;
3.改變一個(gè)字段的默認(rèn)值
alter table table_name alter column_name set default value;
例如:alter table book alter flag set default '0';
4.改變一個(gè)字段的數(shù)據(jù)類(lèi)型
alter table table_name change column column_name column_name type;
例如:alter table userinfo change column username username varchar(20);
5.向一個(gè)表中增加一個(gè)列做為主鍵
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ù)庫(kù)某表的備份,在命令行中輸入:
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.將兩個(gè)表里的數(shù)據(jù)拼接后插入到另一個(gè)表里太惠。下面的例子說(shuō)明將t1表中的com2和t2表中的com1字段的值拼接后插入到tx表對(duì)應(yīng)的
字段里尘吗。
例如:insert into tx select t1.com1,concat(t1.com2,t2.com1) from t1,t2;
10,刪除字段
alter table form1 drop column 列名;