1.首先创建一张表:
create table php313 ( id int primary key auto_increment, //设置id为主键自增长
name char(3) not null default '', //name设为char(3),指的是字符,不论是汉字和是字母,都只能有三个
age tinyint unsigned not null default 0,
email varchar(30) not null default '',
tel char(11) not null default '',
salary decimal(7,2) not null default '1800.68',
date date not null default '2012-03-13' );
建成后表的结构如图:
2.插入
insert into php313 //往哪张表插入
-> (name, age, email, tel, date) //哪些列,如果列不写,则默认插入所有的列。所以必须把全部列手工赋值。包括主键。
-> values
-> ('liubei', '99', 'liubei@shu.com', '13800138000', '2012-12-26'); //插入哪些值
3.修改
update php313 //修改
set //设置
email = 'machao@xilaing.com', //列=内容
salary = 3999.34
where name = 'mac'; //限定哪一行
由于之前我把name设置为char(3)不够存储name,修改为char(10): 使用alter命令:alter table php313 modify name char(10);
现在就可以显示多于3个字符了。
4.删除
delete from php313 where name = 'machao'; //删除马超
总结:查删用from,插入用into,update直接跟表名+set。