MySQL数据库的基础操作

来源:互联网 发布:java aspose 乱码 编辑:程序博客网 时间:2024/05/27 16:43

1.插入数据: 命令:insert into <表名> 【(字段名1).....】values (值n)

                         insert into test (id, name) values(1,'xiaozhang'); 

                         插入id为1,name为xiaozhang的表。

                         insert into test(id,name)  values (2,‘xiaoyu’),(3,xiaoli);

                         批量插入方法,提升效率。

2.查询数据:命令: select <字段1,字段2,....> from <表名> where<表达式>

                       select * from test;

                       查看表test中的所有数据,一般不推荐这样查询,拖慢效率。

                       select  *  from  test limit 2;

                       查询最前面的两行数据。

                       select * from test order by id desc limit 0,2;

                       select * from test order by id asc limit 0,2;

                       以id查询表的第0到2行,desc降序,asc升序。

                       select * from test order where id =2;

                       select * from test order where name =' xiaozhang';

3.修改表中的数据: 命令:update 表名 set 字段=新值,....  where 条件

                       update test set name='xiaoyu' where id =1;

                       把id为1的值的name中的内容修改为xiaoyu。

                       update test set name='xiaoyu' where name='xiaoli' ;

                       update test set name='xiaoyu' ;

                       把name这项的内容全部改为xiaoyu.

4.删除表中的数据:命令:delete from 表名 where 表达式

                        delete from test where id=1;

                        删除表test中编号为1的记录。

                        delete * from test;

                        删除表中的所有数据。

5.在表中增删改字段: 命令:alter  table 表名  add  字段  类型  其他;

                         alter table test add sex char (4);

                         增加性别列sex。

                         alter table test add age int(4) after name;

                         在name列后增加age列。

6.清空表中的内容:命令:truncate table 表名;

                         truncate table test;

                         清空表中的所有内容。

7.更改表名:命令:rename table 原表名 to 新表名;

                         rename table test to mysyu;

                         更改表名为mysyu。

8.删除表:命令:drop table <表名>

                         drop table test;

                          删除表名为test的表。                         

0 0
原创粉丝点击