MYSQL常用命令(3)
白玉 IT哈哈
1、建表
命令:create table ( [,.. ]);
create table MyClass(
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));
2、獲取表結構
命令: desc 表名,或者show columns from 表名
mysql>DESCRIBE MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
3、刪除表
命令:drop table
例如:刪除表名為 MyClass 的表
drop table MyClass;
4、插入數據
命令:insert into [( [,.. ])] values ( 值1 )[, ( 值n )]
例如,往表 MyClass中插入二條記錄, 這二條記錄表示:編號為1的名為Tom的成績為96.45, 編號為2 的名為Joan 的成績為82.99,編號為3 的名為Wang 的成績為96.5.
insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
5、查詢表中的數據
1)、查詢所有行
命令: select from < 表名 > where < 表達式 >
例如:查看表 MyClass 中所有數據
select from MyClass;
2)、查詢前幾行數據
例如:查看表 MyClass 中前2行數據
select from MyClass order by id limit 0,2;
或者:
select * from MyClass limit 0,2;
6、刪除表中數據
命令:delete from 表名 where 表達式
例如:刪除表 MyClass中編號為1 的記錄
mysql> delete from MyClass where id=1;
7、修改表中數據
update 表名 set 字段=新值,… where 條件
update MyClass set name='Mary' where id=1;
8、在表中增加字段
命令:alter table 表名 add字段 類型 其他;
例如:在表MyClass中添加了一個字段passtest,類型為int(4),默認值為0
alter table MyClass add passtest int(4) default '0'
9、更改表名
命令:rename table 原表名 to 新表名;
例如:在表MyClass名字更改為YouClass
rename table MyClass to YouClass;
更新字段內容
update 表名 set 字段名 = 新內容
update 表名 set 字段名 = replace(字段名,'舊內容','新內容');
文章前面加入4個空格
update article set content=concat(' ',content);