mysql必知必会(一)

来源:互联网 发布:php实现跨域 编辑:程序博客网 时间:2024/06/05 06:09

先介绍mysql中的插入、更改、删除操作。分别对应的关键字是insert、update、delete。
假设有个数据库名为book,有张表为user。
create table user(
id int not null auto_increment,
name varchar(20) not null,
password varchar(20) not null,
note text null,
primary kry(id)
)engine=MyISAM;
1.插入数据库(insert)
插入单行:
插入的格式为
insert into user(name,password,note) values('tom','123456a','tom user');
id自增可以不写,因为note字段可以为null,如果没有值也可以省略。
insert into user(name,password) values('tom','123456a');
或insert into user(name,password,note) values('tom','123456a',null);

插入多行:
插入多行时,插入的值用’,’隔开。

insert into user(name,password,note)values('jim','ascvfb',null),('lily','32q1456','it's beautifull'),('lin','532fg',null);

插入检索出的数据(insert select):
新建一个user_new表,字段一样。

insert into user_new (id,name,password,note) select id,name,password,note from user;

2.更新数据库(update)
格式如下:
更新一列

update user set name='lusi' where id=2;

更新多列

update user set name='lusi',password='34512d' where id=2;

如果想删除某列的值,且此值可为null。

update user set note=null where id=2;

3.删除数据(delete)
删除某一行

delete from user where id=2

如果省略where字句则删除所有的数据。
删除所有行
可以直接使用

delete from user

但是这样速度比较慢,可以使用truncate

truncate table user;

以上都是删除表中的数据,没有删除表。
删除表

drop table user;

删除表结构和表中的所有数据。
删除数据库:

drop database book;

重命名表
重命名一张表

rename user to user2;

重命名多张表

rename user to user2,       customer to customer_new,       product to product_new;
0 0
原创粉丝点击