MySQL复习值代码知识点一. 创建数据库create database 数据库名; 二. 删除数据库drop database 数据库名; 三. 选择相应的数据库use 数据库名; 四. 创建表create table table_name( id integer primary key, username varchar(20) not null, birthday date ); 或者设置主键也可用:primary key(id)
primary key(id) 2. 删除主键 alter table + 表名+drop primary key; 如果设置了主键自增时,要先删除自增长,然后再删除主键; alter table + 表名 + modify + 主键 + 列类型; alter table + 表名 + drop primary key; 3. 设置主键并设置主键自增 alter table + 表名 + add primary key(列名); alter table + 表名 + modify + 列名 + 列类型 + auto_increment; 4. 设置外键 foreign key(a_id) references table_A(id) 解释: a_id为本表字段,table_A(id)为外表和外表被引用的字段。 5. 外部添加外键 alter table + 表名 + add[constraint + 外键名字] + foreign key(外键字段) + references + 外部表名(主键字段); 6. 删除外键 alter table + 表名 + drop foreign key + 外键名字; 解释:删除时外键名字是定义时的constraint + 外键名字 的外键名字 如果在删除外键时不知道外键名字,可以用desc+表名来查看表名 7. 添加字段 alter table + 表名 + add +列名+列类型+[约束]; 8. 删除字段 alter table + 表名 + drop column + 列名; 9. 修改列名: alter table + 表名 + change + 列名 + 新列名 + 列类型+[约束]; 10. 修改列属性: alter table + 表名 + modify + 列名 + 列类型 +[约束];
五. 删除表drop table 表名; 六. 表插入数据insert into 表名 values(列值1,列值2,…); 七. 表查询数据select * from 表名;
八. 更新表数据update 表名 set 列名=新值,列名=新值 [where 列名=限定值]; 九. 删除表数据delete from 表名 [where 列名=限定值]; 十. 模糊查询select * from 表名 where 列名LIKE ‘限定值’; 解释: 其中限定值: ”_”为任意单个字符; ”%”为任意个字符(0~+∽); “[]”表示匹配括号内的字符,类似于正则表达式 “[^]”表示不包括括号在内的任意单个字符 十一. 连接结果
select * from 表名1 union select * from 表名2; 解释:union两表的查询结果集列值必须数目相同,否则会出错(1222) 列名显示以左表为主。 union会自动去重 union all不会去重
on后为限制连接的条件,而where是对on筛选后得到的结果集进行再次筛选 交叉连接(笛卡儿积): 查询结果相同: select * from +表名1,表名2; select * from +表名1 cross join表名2[表名3];
内连接(inner join): select * from + 表名1+ inner join +表名2 +[ on 条件][where 条件]; 解释返回两个的交集
左连接(left join): select * from + 表名1+ left join +表名2 +[ on 条件][where 条件]; 解释:以左表为主,不管有没有右表数据,都要显示完全。
右连接(right join) select * from + 表名1+ right join +表名2 +[ on 条件][where 条件]; 解释:以右表为主,不管有没有左表数据,都要显示完全。
参考链接: https://blog.csdn.net/plg17/article/details/78758593 https://blog.csdn.net/cs958903980/article/details/60139792 十二. NULL值处理IS NULL: 当列的值是 NULL,此运算符返回 true。 IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。 查询结果集的修改: select id,ifnull(列名,默认值) from +表名; 解释:如果这一列的值为null 则输出默认值 十三. 正则表达式select * from + 表名+ where + 列名+ regexp +’正则表达式’; 十四. 索引数据库利用各种各样的快速定位技术,能够大大提高查询效率。特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍。 1. 创建索引: 普通索引: alter table + 表名 + add index +索引名 + (列名); create index + 索引名 + on + 表名(列名); 注:主键本来就是一个主键索引(所以在创主键时也是在创建一个索引);
唯一索引: create unique index + 索引名 + on + 表名(列名); 2. 删除索引: drop index + 索引名 + on + 表名; alter table + 表名 + drop index + 索引名; 3. 查看索引: show index from + 表名; show keys from + 表名; 十五. 聚集函数传送门: https://www.cnblogs.com/Anxc/p/11558963.html 十六. 附录还有一部分是关于聚集函数的知识点,我把它放在了我的GitHub里面,由于截图的原因没有发布到这里(请看文章最后文档下载)。 MySQL 5.0 以上的版本: 1、一个汉字占多少长度与编码有关: UTF-8:一个汉字=3个字节 GBK:一个汉字=2个字节 2、varchar(n) 表示 n 个字符,无论汉字和英文,Mysql 都能存入 n 个字符,仅是实际字节长度有所区别 3、MySQL 检查长度,可用 SQL 语言来查看: mysql> select name,length(name) from grade where id=8; +------+--------------+ | name | length(name) | +------+--------------+ | 安 | 3 | 查看表结构: desc 表名 单词翻译: alter:改变 modify:修改(修改列属性) constraint:限制,约束(设置外键名字,删除外键时用) foreign:外国的(设置外键时用) references:参考文献(设置外键时用) primary:主要的(设置主键时用到) regexp:正则表达式 * 文档下载:GitHub:https://github.com/Anxc520/knowledge.git 教程如下:
|
|