Mysql基本使用语句

数据库端口号:Mysql:3306 Orasle:1521 Sqlserver:1443
DML: 数据操作语言(检索或者修改数据)
DDl:数据定义语言(定义数据结构,比如创建、修改或删除数据库的对象)
DCl:数据控制语言(用于定义数据库用户的权限)
DML: Select:--用于检索数据
Insert:--用于增加数据到数据库
Update:--用于从数据库中修改现存的数据
Delete:--用于从数据库中删除数据
DDL: Create table --创建表
Alter --修改表
Drop table --删除表
DCL: COMMIT --提交
ROLLBACK --回滚

中文乱码:支持中文编码格式:utf8 GBK gb2312
查看端口号是否正确:\Program files\mysql\mysql Server 5.5\my.ini中port是3306

mysql-user表 host:代表可连接的主机
% --所有电脑都可以连接
127.0.0.1 --主机或IP地址只能输入127.0.0.1,只能本地连接
localhost --主机或IP地址只能输入localhost,只能本机连接

创建表必须具备的条件:create table的权限 ; 存储空间

DDL 语句修改表: 追加新的列语法:alter table 表名 add 列名 类型(长度);
修改字段类型语法:alter table 表名 modify 列名 类型(长度);
修改字段名称语法:alter table 表名 change 旧列名 新列名 类型(长度);
删除一个列语法:alter table 表名 drop 列名;

DDL语句删除表语法:drop table 表名
数据和结构都被删除
所有正在运行的相关事务被提交
所有相关索引被删除
DROP TABLE 语句不能回滚
Mysql的两种主键。Primary key 和not null auto_incriment
在建立mysql表时,给一个字段添加了主键primary key
Alter table 表名 add primary key(id);
Alter table 表名 change id id int(10) not null auto_increment=1;

删除自增长的主键id
先删除自增长在删除主键 Alter table 表名 change id id int(10);//删除自增长
Alter table tb drop primary key;//删除主建

DML查询:基本查询:Select * from 表名
Select * from 表名 where 条件
Select * from 表名 where 字段名> 条件
模糊查询:Select * from 表名 where 条件 like '%字段%'
逻辑运算符: and or not in
条件查询:Select * from 表名 where 列名 between 数值 and 数值
Select * from 表名 where 条件 in(数值,数值)
Select * from 表名 where 条件 is not null

排序查询:Select * from 表名 order by 条件 asc/desc(升序/降序)


分组函数的使用:
AVG()求平均值: select avg(列名) from 表名;
SUM()求和: select sum(列名) from 表名;
MAX()求最大值: select max(列名) from 表名;
MIN()求最小值: select min(列名) from 表名;
COUNT()求个数: select count(列名) from 表名;

GROUP BY手动分组:
GROUP BY单独使用语法:select 列名 from 表名 group by 列名;
GROUP BY和分组函数一起使用:select 列名,avg(列名) from 表名 group by 列名;

去重复语法:Select distinct 字段名 from 表名;

having分组以后过滤语法:select 列名,count(*) from 表名
where 条件>数值 --分组前过滤
group by 列名
having count(*)>数值; --分组后过滤

子查询语法:Select * from 表名 where 字段名()
内连接语法:select 别名.字段名,别名.字段名, from 表名 别名 where 别名.字段名=别名.字段名
多表查询(其他连接)语法:
select 别名.字段名,别名.字段名, from 表名 别名 (inner on/right/left) join 别名.字段名=别名.字段名

猜你喜欢

转载自www.cnblogs.com/wen6/p/11107288.html
今日推荐