首页  >  新闻动态  > 网站知识 > 数据库

数据库基本操作

一、数据库的基本操作

1、查看所有的数据库:show databases;

2、创建数据库:create database 数据库名字;

3、选中数据库:use 数据库名字;

4、删除数据库:drop database 数据库名字;

二、表的操作

1、查看所有的表:show tables;

2、创建表:create table 表名 (字段1 数据类型1,字段2 数据类型2……)

3、修改表:alter table

(1)增加字段:alter table 表名 add (字段1 数据类型1,字段2 数据类型2……)

(2)删除字段:alter table 表名 drop 字段1,drop 字段2;

(3)修改字段名称:alter table 表名 change 旧字段 新字段 数据类型;

(4)修改数据类型:alter table 表名 modify 字段 新数据类型;

(5)重命名:alter table 表名 rename 新表名;

4、删除表:drop table 表名;

5、复制表:create table 表名 as select * from 表名2 where 条件;

6、查看表的结构:desc/describe 表名;

三、数据的操作

1、插入数据:insert into 表名 values (值1,值2……)

2、更新数据:update 表名 set 字段=值 where 条件

3、删除数据:delete from 表名 where 条件

4、查找数据:select */字段 from 表 where 条件

(1)条件:>、<、=、!=、<>、>=、<=、in (值1,值2……)、not in (值1,值2……)、between and、like %

(2)聚焦函数:sum、count、avg、max、min

(3)分组:select 字段,聚焦函数 from 表 group by 字段

(4)连接:

内连接:join、 where

select */字段 from 表1 join 表2 on 条件1 join 表3 on 条件2;

select */字段 from 表1,表2…… where 条件1 and 条件2;

外连接:left、right

select */字段 from 表1 (left/right) join 表2 on 条件1 (left/right)join 表3 on 条件2;

(5)合并:select */字段 from 表1 union select */字段 from 表2……;

(6)查找唯一性数据:select distinct */字段 from 表 where 条件;

(7)限制查找:

查找前n行:select */字段 from 表 limit n;

查找a+1行a+b行:select */字段 from 表 limit a,b;

(8)排序:select */字段 from 表 order by 字段 (升序asc/降序desc)

五、索引和视图

(一)索引

1、创建索引

(1)在已创建的表里创建索引:create index 索引名 on 表名(字段);

(2)在创建表的时候创建索引:create table 表名 (字段1 数据类型1,字段2 数据类型2……,d xsxxvs

index 索引名(字段));

(3)alter table 创建索引:alter table 表名 add (index 索引名1(字段1),index 索引名2(字段2)……);

2、查看索引:show index from 表名;

3、删除索引:drop index 索引名 on 表名;

同时删除多个索引:alter table 表名 drop index 索引名1,drop index 索引名2;

(二)视图

1、创建视图:create view 视图名 as select */字段 from 表 where 条件

2、查找视图:select */字段 from 视图 where 条件

3、删除视图:drop view 视图名字

六、函数

取字符函数

1、substring(字段,a,b):从第a个字符开始,取b个字符

2、left(字段,n):从左边取n个字符

3、right(字段,n):从右边取n个字

相关文章
http://www.aspxadmin.com/Content/weixinlogo.jpg
阅速CMS系统提供用户体验更佳的国内Cms软件
数据库基本操作