一、數據庫常用命令
1、幫助
mysql> help contents;
2、連接與關閉
mysql -u -p -h -P
3、創建/刪除數據庫
create database [if not exists] dbName; //創建
drop database dbName; //刪除
4、統計數據庫信息
show table status [{from|in} db_name] [like 'pattern'|where expr] \G
將輸出Mysql數據庫管理系統的性能及統計信息。
如:
show table status from RUNOOB; #顯示數據庫 RUNOOB 中所有表的信息
show table status from RUNOOB like 'runoob%'\G #表名以runoob開頭的表的信息
5、顯示錶結構
desc tableName;
6、顯示錶索引
show index from
7、創建表
create table tableName(列聲明);
如:
create table students(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null
);
8、修改表中內容(數據)—增、改、刪
1)增:insert
insert [into] tableName [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...)
2)改:update
update tableName set 列名稱=新值 where 更新條件
3)刪:delete
delete from tableName where 刪除條件
4)批量刪:drop、truncate
drop table tableName # 刪除表(包括表的結構和全部數據)
truncate tableName # 刪除表(刪除表裏的全部數據,保留表結構)
9、創建後表的修改alter table
1)添加列
alter table 表名 add 列名 列數據類型 [after 插入位置];
2)修改列
alter table 表名 change 列名稱 列新名稱 新數據類型;
3)刪除列
alter table 表名 drop 列名稱;
4)重命名錶
alter table 表名 rename 新表名;