mysql常用基本语句一

mysql结合php进行中小型网站开发是一个应用非常广泛的技术。

mysql有哪些常用基本语句?只要百度一下就可以找到很多,但是我还是要记录一下在我自己的随笔里!

1.创建数据库

create database databsename;

 例如:

create database mydata;

 2.创建表

create table tablename(字段1 字段属性,字段2 字段属性);

 例如:

create table msg(id int(10) primary key auto_increment,title varchar(60) not null default '',name varchar(10) not null default '',content varchar(500));

 3.增删查改

3.1.增 向表中插入数据

insert into 表名(字段1,字段2,字段3...)values('值1','值2','值3',....);

 //其中字段名的位置要和值的位置一一对应,如果字段是字符型的数据,那么必须要加半角英文输入法的单引号或者双引号,还有一些其他的数据类型要加的,反正全部加就不会错了!

例如:向msg表中插入一条数据 有主键的表

insert into msg(title,name,content)values("hello","jim","I am a student!");

 向msg表中插入多条条数据 有主键的表

insert into msg(title,name,content)values("hello","jim","I am a student!"),("good","tom","I am a teacher!"),("good","tom","I am a teacher!");

 3.2 删

delete from 表名 where 条件表达式;

 例如:在msg表里面把id等于1的那条数据删除

delete from msg where id=1;

 把msg表里面的数据全部删除,在执行删除操作的时候要非常非常地小心,因为你如果少写一个where子句就会把全部数据删除,到那时候就会欲哭无泪!

delete from msg;

 3.3 查

select 字段1,字段2,字段3,... from 表名 where 条件表达式或者其他查询字句;

 例子:

select * from msg; --把msg表的所以内容查找出来
select * from msg where id=2; --把id=2那条数据查找出来
select name from msg where id=3; --把id=3的那条数据的name字段内容查找出来

 3.4 改

update 表名 set 字段名='要修改的值' where 条件语句;

 例子:

update msg set name='kongfu' where id=3; --把id=3的那条数据的name字段的值改为kongfu
update msg set name='kongfu'; --把表中的所有行的name字段值改为kongfu

 在实际开发中修改数据的时候也要特别注意!

 

本次主要是记录了一些非常非常基本的DDL语句和DCL语句。

 

 

 

posted @ 2012-11-18 00:06  kongfu  阅读(209)  评论(0编辑  收藏  举报