# 查看数据库中所有表的名称
db_bank:查看的数据库名称
select table_name from information_schema.tables where table_schema='%s' and table_type='base table'" % db_bank
# 查询以sto开头的表
show tables like 'sto%';
------------------------------------------------------------
原文:http://www.cnblogs.com/zhangzhu/archive/2013/07/04/3172486.html
---------------------------------------数据库操作------------------------------------------------------------
# 创建数据库test
create database test
# 删除数库test
drop database test;
# 显示数据库
show databases;
-------------------------------------------表操作------------------------------------------------------------
# 创建表
create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);
create table t_test(
id int(4) not null primary key auto_increment,
name char(20) not null,
sex int(4) not null default '0',
degree double(16,2));
# 删除表
drop table t_test
修改表名
rename table 原表名 to 新表名;
如:在表MyClass名字更改为YouClass
rename table MyClass to YouClass;
-----------------------------------------表字段操作----------------------------------------------------------
# 增加字段
alter table 表名 add字段 类型 其他;
如:在表t_test中添加了一个字段passtest,类型为int(4),默认值为0
alter table t_test add passtest int(4) default '0'
加索引
alter table 表名 add index 索引名 (字段名1[,字段名2 …]);
例子:alter table employee add index emp_name (name);
加主关键字的索引
alter table 表名 add primary key (字段名);
例子:alter table employee add primary key(id);
加唯一限制条件的索引
alter table 表名 add unique 索引名 (字段名);
例子:alter table employee add unique emp_name2(cardnumber);
alter table 表名 drop index 索引名;
例子: alter table employee drop index emp_name;
修改原字段名称及类型:
ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
删除字段:
ALTER TABLE table_name DROP field_name;
-------------------------------------------表数据操作------------------------------------------------------增:
insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
# 增加一条数据
insert into t_test values('1','root')
# 增加多条数据
insert into t_test(id,name) values('1','root'),('2','root1');
删:
如:删除表 t_test中id为1 的记录
delete from t_test where id=1;
# 删除t_test表中所以的记录
delete from t_test
改:
update t_test set name='Mary' where id=1;
查:
如:查看表 t_test 中前2行数据
select * from t_test order by id limit 0,2;