您的位置:首页 > 数据库 > MySQL

MySql 系列数据库详解

2016-10-05 14:15 260 查看
数据类型:文本,数字,日期







/**

 * Likie

 * 重启

 * Linux(CentOs):/etc/init.d/mysqld restart

 *

 * 命令:

 * DB:数据库

查看当前有哪些DB:show databases;

show databases;

添加DB:create database gc;

create database gc;

删除DB:drop database gc;
drop database gc;

table:数据表

选择数据库DB:(成功返回:Database changed)
use gc

查看数据表:show tables;show tables;
查看表结构(查看字段):descride 表名;
descride table_name

删除表:drop table 表名;
drop table table_name;


字段操作:

增加字段(增加列):alter table 表名 add 字段名 int(11) not null default 1;
alter table table_name add id int(11) not null default 1;


删除字段(删除列):alter table 表名 drop 字段名;
alter table table_name drop id;


修改字段名(修改列名)(数据类型不变):alter table 表名 change 原字段 新字段 varchar(255);

修改字段数据类型(修改列人数据类型):alter table 表名 change 字段 字段 新数据类型;

修改字段名和数据类型:alter table 表名 change 原字段 新字段 新数据类型;

表名操作:

修改表名:alter table 原表名 rename 新表名;

创建数据表及字段:

CREATE TABLE 表名(

id bigint(20),

caeate Time datetime,

ip varchar(255),

mobile varchar(255),

nickname varchar(255),

passwd varchar(255),

brief text,

);

删除表:drop table 表名

 *

 *

 */

/**

SQL查询语句:

select * from 表名;

select 字段,字段2,字段3 from 表名;

SQL插入数据:

insert into 表名 value(值1,值2,...)

insert into 表名 (字段1,字段2)value('值1','值2')

where语法

select 字段或* from 表名 where 字段='字段值';

where高级(in)语法,多值或值为查询后

select * from 表名 where 字段 in(值1,值2);

select * from 表名 where 字段 in(select 字段 from 表名);

where高级(between)之间

select * from 表名 where between id 1 and 3; 查询id为1到3之间的数据

select * from 表名 where not between id 1 and 3; 查询id不在1到3之间的数据

where高级(like)匹配:%:通配符:%123%:包函123,%123或最后函有123

select * from 表名 where 字段 like '%123%'; 查询字段值包括123的信息

select * from 表名 where 字段 not like '%123%'; 查询字段值不为123的信息

and:和、or:或者

select * from 表名 where 字段=值 and 字段2=值2

括号结合运算:

select * from 表名 where 字段=值 and (字段2=值2 or 字段3=值3);

查找为空的数据:

slectr * from 表名 where 字段 is null;

查询不为空的数据:

select * from 表名 where 字段 is not null;

去重:distinct(字段内容重复的直接过滤)

select distinct 字段,字段2 from 表名

order by:排序 asc与desc,默认asc

单按字段(列)排序:

select * from 表名 where 字段1=值1 order by 字段 asc或desc;

多字段(列)排序:先按第一个字段要求排序,如果第一个字段有相同内容再用第二字段来排序

select * from 表名 where 字段2=值2 order by 字段 asc或desc,字段2 asc或desc;

limit:查询数量

select * from 表名 where 字段=值 order by 字段 desc limit [起始,]多少条记录;

insert into 与 select组合使用:将查询结果全部插入表1

insert into 表1 select * from 表2 where 字段字!=值1

修改更新数据:updete

单个字段值修改:update 表名 set 需要修改的字段=修改成的值 where 字段=值3;

多个字段值修改:update 表名 set 需要修改的字段=修改成的值,需要修改的字段2=修改成的值2 where 字段=值3;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: