一.数据库操作
1.新建数据库
>create database du_name; //du_name是数据库名
2.查看数据库
>show databases;
3.使用数据库
>use du_name; //du_name是数据库名
4.删除数据库
>drop database du_name;
二.创建表
1.创建表
>create table tb_name( //tb_name是表名
字段/列名 数据类型(长度) 约束条件,
字段/列名 数据类型(长度) 约束条件,
字段/列名 数据类型(长度) 约束条件
)约束条件;
例;>create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..);
2.查看数据库可用表
>show tables;
3.查看表结构
>database tb_name;
4.复制表
>create table tb_name2 select*from tb_name;
部分复制
>create table tb_name2 delect id,name from tb_name;
5.表重名
>rename table name_old to name_new;
6.删除表
>drop table ta_name; //tb_name是表名
7.增加列
>Alther table tb_name add colum col type;
8.添加主键
>Alter table tabname add primary key(col);
9.删除主键
>Alter table tabname drop primary key(col);
10.选择数据
>select * from table1 where 范围;
11.插入数据
>insert into table1(field1,field2) values(value1,value2);
12.删除表中数据
>delete from table1 where 范围;
13.更新数据
>update table1 set field1=value1 where 范围;
14.查找
>select * from table1 where field1 like ’%value1%’;
15.排序
>select * from table1 order by field1,field2 [desc];
16.总数
>select count as totalcount from table1;
17.求和
>select sum(field1) as sumvalue from table1;
18.平均
>select avg(field1) as avgvalue from table1;
19.最大
>select max(field1) as maxvalue from table1;
20.最小
>select min(field1) as minvalue from table1;