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的