mysql笔记

1. 显示mysql中全部数据库的名称

SHOW databases;
 SHOW schemas;

2. 显示当前数据库中全部表的名称

SHOW tables;//显示当前数据库的全部的表        
SHOW tables from database_name;//显示某个数据库的全部的表

3. 显示表的全部列

SHOW columns from table_name from database_name;
SHOW columns from database_name.table_name;

4. 其它相关

SHOW grants; // 查看权限 
SHOW index from table; //查看索引
SHOW engines; // 显示安装之后可用的存储引擎和默认引擎。

5 显示建立数据库的结构 表的结构

SHOW create database test;
SHOW create table student;

6. 检索数据

SELECT name from student;       //显示学生表的全部姓名

SELECT name,age from student;    //显示学生表的全部姓名,年龄

SELECT * from student;          // 显示学生表的全部

表级别的增删改查

/*
*@列操做
*/

ALTER table cup add column age int not null;            //增列
ALTER table cup drop column name;                       //删列    
ALTER table cup modify column name varchar(40) not null //改列
ALTER table cup change age age int(2) not null;     //change能够替换列的名字 

ALTER table cup modify id int(10) default 0 first;  //把id移动到开头 
ALTER table cup modify id int(10) default 0 last;  //把id移动到最后
ALTER table cup modify name varchar(20) after id;  //移动位置 

SHOW columns from cup;                              //查看列 

/*
*@约束操做 primary key 主键 not null 非空 unique 惟一约束 
*/
ALTER table cup add primary key(id); // 增主键
ALTER table cup drop primary key;    //删主键

数据的增删改查

// 字段若是是一一对应,能够删掉,若是顺序不对或者数量不对要加上
insert into cup (id,name,counts,age) values (1,"lili",22,22)

// 条件删除
delete from cup where id = 1;

// 删除表的所有数据,表还有,可是数据是空的
delete from cup;

// 改数据
update from cup set name="huahua" where id = 1;

//查询数据
SELECT name from cup;

SELECT具体用法

/**
*@简单查询
*/
SELECT name from cup; //查1行

SELECT name,age from cup; //查2行

SELECT * from cup; //查全部行

/**
*@限制查询返回的数量
*/
SELECT * from cup limit 2; //只要前两条 

SELECT * from cup limit 2 offset 1  ; //从第1条开始取2条


/**
*@where 条件判断
*/
SELECT * from cup where id = 1;
SELECT * from cup where id > 1;
SELECT * from cup where id >= 1;
SELECT * from cup where id < 1;

////  范围筛选 

// id 1,2,3均可以
SELECT * from cup where id in (1,2,3);
// id 不是1,2,3均可以 
SELECT * from cup where id not in (1,2,3);

// id 大于1,2,3 的全部,就是大于所有才能够 
SELECT * from cup where id > all  (1,2,3);
SELECT * from cup where id >= all (SELECT id from cup);
 
// id 大于1,2,3中的1个就能够和下边一句是同样的 some和any同样用 
SELECT * from cup where id > some  (1,2,3);
SELECT * from cup where id > 1 or id > 2 or id > 3;

/**
*@合并查询结果
*union会删除重复的数据,union all不会删除重复的 
*/
// cup1和cup2的字段必须同样,若是不同就合并同样的
SELECT * from cup1 union SELECT * from cup2;



/**
*@模糊查询
*/
SELECT name from cup where name LIKE "hua%"; // hua开头的
SELECT name from cup where name LIKE "%hua%"; // 中间有hua的
相关文章
相关标签/搜索