博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
复习mysql
阅读量:7172 次
发布时间:2019-06-29

本文共 2383 字,大约阅读时间需要 7 分钟。

#1. 操作文件夹

    增:create database db1 charset utf8;

    查:show databases;

    改:alter database db1 charset latin1;

    删: drop database db1;

 

#2. 操作文件

    先切换到文件夹下:use db1

增:create table t1(id int,name char);

复制表结构+记录(除了key): create table new_t1 select * from t1;

只复制表结构: create table new_t1 select * from t1 where 1=2;

               Create table new_t1 like t1;

create table 表名(

字段名1 类型[(宽度) 约束条件],

字段名2 类型[(宽度) 约束条件],

字段名3 类型[(宽度) 约束条件]

);

#注意:

1. 在同一张表中,字段名是不能相同

2. 宽度和约束条件可选

3. 字段名和类型是必须的

 

查:show tables

 

    改:alter table t1 modify name char(3);

       alter table t1 change name name1 char(2);

       alter table t1 rename t2;

       alter table t1 add id int, name char(16) not null;

        alter table t1 add age int(3) not null default 22 first;

       alter table t1 add sex enum(‘male’, ‘female’) default ‘male’after id;

       alter table t1 drop name;

       alter table t1 drop primary key;

        alter table service engine = innodb;

 

    删:drop table t1;

    

#3. 操作文件中的内容/记录

增:insert into t1 values(1,'egon1'),(2,'egon2'),(3,'egon3');

    Insert into t1(name) values(‘egon1’);

    查:select * from t1;

    改:update t1 set name='sb', ** = **, where id=2 and **=**;

    删:delete from t1 where id=1;

 

    清空表:

    delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。

    truncate table t1;数据量大,删除速度比上一条快,且直接从零开始,

 

#4. 联合唯一

create table service(

id int primary key auto_increment,

name varchar(20),

host varchar(15) not null,

port int not null,

unique(host,port)); #联合唯一

 

#5. 多列主键

create table service(

ip varchar(15),

port char(5),

service_name varchar(10) not null,

primary key(ip,port)

);

 

#6. Foreign key

create table author2book(

id int not null unique auto_increment,

author_id int not null,

book_id int not null,

constraint fk_author foreign key(author_id) references author(id)

on delete cascade

on update cascade,

constraint fk_book foreign key(book_id) references book(id)

on delete cascade

on update cascade,

primary key(author_id,book_id)

);

 

#7. 单表查询

SELECT DISTINCT <select_list>

FROM <left_table>

<join_type> JOIN <right_table>

ON <join_condition>

WHERE <where_condition>

GROUP BY <group_by_list>

HAVING <having_condition>

ORDER BY <order_by_condition>

LIMIT <limit_number>

关键字的执行优先级

From

On (join condition )

Join

where

group by

having

select

distinct

order by

limit

 

#8 多表查询

全外连接:

select * from employee left join department on employee.dep_id = department.id

union

select * from employee right join department on employee.dep_id = department.id

;

转载于:https://www.cnblogs.com/yangli0504/p/9205751.html

你可能感兴趣的文章
补天白帽大会:安全不只是防御 是持续运营的过程
查看>>
特朗普遭抗议:美国投资人仍看好可再生能源
查看>>
[译] 通过后台数据预获取技术实现性能提升
查看>>
iOS实现多继承的几种方式
查看>>
我要做 Android 之要点总结
查看>>
Http请求与响应
查看>>
白山云科技校招:系统研发、机器学习、数据挖掘工程师
查看>>
SpringBoot实现定时任务的几种方式(常用)
查看>>
Web请求过程
查看>>
LRU 缓存淘汰算法的两种实现
查看>>
sql经典题目
查看>>
解决异常:SAXParseException在文档的元素内容中找到无效的 XML 字符
查看>>
H5移动端开发常见问题以及解决办法
查看>>
Jenkins 版本发布
查看>>
在下一次你的系统出问题前该准备的一点事情
查看>>
Android & Java8
查看>>
【Android SDK】在命令行管理Android SDK
查看>>
618 | “赢销备战”,全场景助力商家!
查看>>
React 基础知识总结
查看>>
ANGULAR JS常用指令NG-IF、NG-CLASS、NG-OPTION、NG-VALUE、NG-CLICK是如何使用的?
查看>>