Mysql 语法总结(1)

打开数据库: 1. net start mysql 2. mysql -uroot -proot ; 3.root

关闭数据库: net stop mysql ;

创建库 : create database name1;

删除库 : drop database namel;

引用库 : use name1;

创建表格: create table name1(元素名 元素类别, 元素名 元素类别....);

显示库内所有元素 : show databases ;

显示所有表格 : show tables ;

查询表格内容 : select * from namel;

查询正在使用的库 : select database();

删除表格 : drop table namel;

查看表格的结构: describe namel; 可简写:desc namel ;

更改表格结构: alter table namel add(或者其他操作) column(列) namel 属性的数据类型; <---本句是添加一列表格.

添加主键 : alter table name1 modify ID名 id类型 primary key auto_increment;

向表格内增加数据 : insert into namel values('##','##');

删除数据 : delete from namel where FieId ;

修改某一条数据 : update namel set 属性名 = '新值' where id='序列的值';

(注: namel 是假设的库名)

*****alter更改表结构 (alter:改变性质或成分)(modify:修改)(character:字符)(column:列)

给表添加一列:
alter table 表名 add column 属性名 属性数据类型;(column可加可不加)
添加主键:
alter table 表名 modify ID名 id类型 primary key auto_increment;
更改列名:
alter table 表名 change 旧列名 新列名 新列名的数据类型;
删除列:
alter table 表名 drop column 列名;(column可加可不加)
修改表名:
rename table 表名 to 新表名;
修改列的属性:
alter table 表名 modify 列名 类型;
修改表对的字符集:
alter table 表名 character set 字符集;

*****select查询
(1)select distinct语句用于返回唯一、不同的值。在表中一个列可能会有多个重复值,有时需要列出不同的值。
格式: select distinct 列名 from 表名;

(2)别名查询 as 列名查询后,给查询出来的值再起一个别名,只是临时的,不会存入到数据库
格式: select 列名 as 起的名字 from 表名;

(3)对列进行运算查询 ‘+ - * /’ 需要对某一列数值进行运算查看时,如计划对工资进行涨或降时,不存入到数据库
格式: select 列名+数值 from 表名

(4)条件查询:
1、where 格式: select 字段 from 表名 where 条件

2、 > 、 < 、 = 、<= 、>=
格式:(1)select * from 表名 where money>1000(判断条件);
(2)select money>1000(判断条件) from 表名; (符合条件返回1,不符合返回0)

3、between...and 显示在某一区域的值,含头、含尾
格式:select * from 表名 where 列名 between 条件1 and 条件2;

4、like 模糊查询 * like %a% %为通配符,和所有字符都匹配
* like "_" _ 一个下划线代表一个字符
格式: select * from 表名 where 列名 like '%a%';

5、is null 判断是否为空
格式: select * from 表名 where 列名 is null;

(5)逻辑运算符
and 多个条件同时成立
格式: select * from 表名 where 条件1 and 条件2;
or 多个条件任一个成立
格式: select * from 表名 where 条件1 or 条件2;
not 不成立

(6)聚合函数
count()
格式:select count(*) from 表名 where 条件;
select count(*) from 表名;

原文地址:https://www.cnblogs.com/550410638boke/p/10621427.html