【MySQL】基础语句

文章目录


前言

mysql基础语句记录


内容

1.创建

create table <table-name> (field1 type1,field2 type2,.....);

2.修改\删除

修改表名   alter table <table-name> rename to <table-name>;
修改表字段名称 ater table <table-name> change <old-field> <new-field> <数据类型> ;
修改表字段类型 alter table <table-name> modify <fiield> <数据类型> ;
删除字段 alter table <table-name> drop <field> ;
删除表   drop table if exists <table-name> ;

3.外键约束-创建于删除

创建外键约束    table1-id1   table2-id2
constraint  field  foreing key table2-id2 references table2 <id1 >  在创建表时使用
解除外键约束
alter table <table-name> drop foreing key field ;
  1. 添加新字段
alter table <table-name> add <field> <type> ;  (在表尾添加)
alter table <table-name> add <field> <type> first;  (在表头添加)
alter table <table-name> add <field> <type> alter <field>;(在指定字段后添加)

5.约束、主键、外键

定义字段时指定主键      filed type primary key ,
修改表时添加主键     alter table <table-name> add primary key <field>;
删除主键     alter table <table-name> drop primary key;
主键自增长    AUTO_INCREMENT	   指定自增长位置----AUTO_INCREMENT = 100
修改表时添加外键约束	alter table <table-name> add constraint  <外键名> foreing key <field>  references <table-name> <field>;
唯一约束    <field> <type> unique ;
在修改表时添加唯一约束    alter table <table-name> add constraint  <唯一约束名> unique <field>;
删除唯一约束    alter <table-name> drop index <唯一约束名>;
默认值约束   <field> <type> DEFAULT <默认值>;
alter table <table-name>  change column <field> <type> default <默认值>;
默认值非空约束    将默认值约束的 默认值  换成NOT NULL

6.运算符

 +
 -
 * 
/ 
%,	(MOD)
逻辑运算符
NOT!AND&&OR||XOR   逻辑异或

7.操作表中数据

查询数据   select <field> from <table-name>;
过滤重复数据
select distinct <field> from  <table-name>;
返回不重复的字段数
select count(distinct <field1> <field2>) from  <table-name>;
给数据表设置别名
select <别名>.<field> from  <table-name> as <别名>;
给字段设置别名
select <field> as <别名> from  <table-name>;
限制查询结果的条数
select * from  <table-name> limit <开始位置> <查询条数>;
select * from  <table-name> limit  <查询条数>;
select * from <table-name> limit<查询条数> offset <开始位置>;	
对查询结果排序
select * from from <table-name> order by <field> <降序 DESC> <升序 ASC>;
模糊查询   ----可以匹配大小写--- LIKE BINARY 't%'
HAVING 关键字和 WHERE 关键字都可以用来过滤数据,且 HAVING 支持 WHERE 关键字中所有的操作符和语法。
selcet * from <table-name>  where <fielf> like '%T%';    中间含有字母T的
selcet * from <table-name>  where <fielf> like '_T_';      只有三个字母且T在中间的
selcet * from <table-name>  where <fielf> not like '%T%'; 不含字母T的
范围查询   [NOT]BETWEEN AND
select * from <table-name> where <field>[判断目标] between <条件1>and <条件2>;
select * from <table-name> where <field>[判断目标] not between <条件1>and <条件2>;
空值查询
 select * from <table-name> where <field>[判断目标] is [not] null;
分组查询
select <field> from <table-name>  group by <field>;
正则表达式查询
select * from <table-name> where <field> regexp '<正则>';
插入数据
insert into <table-name> <field> values <value>;
insert into <table-name> set <field>=<value>,<field>=<value>;
insert into <table-name> (<field>,<field>) values(<value>,<value>);
 修改操作
UPDATE <表名> SET 字段 1=1 [,字段 2=2] [WHERE 子句 ]
[ORDER BY 子句] [LIMIT 子句]
删除数据
DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]

8.用户操作

创建用户
create user <username> indentified by <password>;
create user 'test'@'localhost' indentified by '123';
修改用户
rename user <old-user> to <new-user>;
rename user 'test'@'localhost' to 'test1'@'localhost';
删除用户
drop user <username>;
drop user 'test1'@'localhost';

delete from mysql.user where Host ='hostname' and user = <username>;
delete from mysql.user where Host = 'localhost' and user = 'test1';
登录数据库
mysql -h localhost - u root -p password;
root修改普通用户密码
set password for 'username'@'hostname' = password("newpass");
update mysql.user set authentication_string = password('newpass') where user = 'root' and host = ' localhost';

总结

简单记录语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值