计较多了,心情累了;欲望大了,幸福少了。人生百味,繁华万千,终是过眼云烟,昙花一现。
腾讯大王卡免费30G专属流量

mysql数据库基础知识(Mysql入门知识)

Sql语言的分类--功能

DDL:用来创建或者删除数据库,表,数据库操作语句DML:用来对表中的内容,进行增删改的,数据库中数据的操作语句DQL:用来对表中的数据进行查询操作DCL:权限管理语句database:数据库table:表格(数据库的表)show:展示

DDL

创建数据库:

create database mydb1;-- 创建数据库mydb1

删除数据库:

drop database mydb1; -- 删除数据库mydb1

使用数据库:

use 数据库名称;

创建student表:

create table 表名(字段1 字段类型 约束,字段2 字段类型 约.......)

删除表:

drop table 表名;

修改表名称:

rename table 旧表名 to 新表名; --注意:如果有外键,需要更新外键

修改表结构

添加列:

alter table 表名 add 列名 类型

删除列:

alter table 表名 drop column 列名

修改列:

alter table 表名 modify 列名 类型; -- 修改类型

alter table 表名 modify column 列名 类型; -- 类型

alter table 表名 change 原列名 新列名 类型; -- 列名,类型(可以改名字)

添加主键:

alter table 表名 add primary key(列名);

删除主键:

alter table 表名 drop primary key; alter table 表名 modify 列名 int(原本类型), drop primary key;

添加外键:

alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);

注意:创建外键时,主表链接附表的(主键字段)且字段类型必须相同;

[](javascript:void(0); "复制代码")

<code-box id="code-7AbNTi" style="padding: 0px; margin: 5px; position: relative; display: block; border-radius: 4px; box-shadow: rgba(37, 44, 97, 0.15) 0px 4px 11px -2px, rgba(93, 100, 148, 0.06) 0px 1px 3px 0px;"><pre boxid="code-7AbNTi" style="padding: 10px; margin: 0px; overflow: auto; font-size: 12px !important; font-weight: 400; font-family: "Source Code Pro", Consolas, Menlo, Monaco, "Courier New", monospace !important; white-space: pre; border-radius: 0px 0px 4px 4px; word-break: break-all; overflow-wrap: break-word; counter-reset: itemcounter 0; color: rgb(35, 38, 59); background: rgb(245, 245, 250);">-- 附表创建

create table clazz(id int primary key ,clazz_name varchar(20)); -- 创建主表(带外键)

create table student

(id int primary key auto_increment,name varchar(20),

clazz_id int ,foreign key (clazz_id) references clazz(id)); -- 删除外键

alter table

-- 因为没有使用 constraint 关键字给外键起名

-- 所以student_ibfk_1(这是默认外键名)

student drop foreign key student_ibfk_1; -- 增加外键

alter table student add

-- 给外键起名clazz_id

constraint clazz_id foreign key (clazz_id) references clazz(id);</pre></code-box>

[](javascript:void(0); "复制代码")

删除外键:

alter table 表名 drop foreign key 外键名称

修改默认值:

ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;

删除默认值:

ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

DML

一、增

insert into 表 (列名,列名...) values (值,值,...) insert into 表 (列名,列名...) values (值,值,...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表

<code-box id="code-FTyEPK" style="padding: 0px; margin: 5px; position: relative; display: block; border-radius: 4px; box-shadow: rgba(37, 44, 97, 0.15) 0px 4px 11px -2px, rgba(93, 100, 148, 0.06) 0px 1px 3px 0px;"><pre boxid="code-FTyEPK" style="padding: 10px; margin: 0px; overflow: auto; font-size: 12px !important; font-weight: 400; font-family: "Source Code Pro", Consolas, Menlo, Monaco, "Courier New", monospace !important; white-space: pre; border-radius: 0px 0px 4px 4px; word-break: break-all; overflow-wrap: break-word; counter-reset: itemcounter 0; color: rgb(35, 38, 59); background: rgb(245, 245, 250);">例: insert into tab1(name,email) values('zhangyanlin','zhangyanlin8851@163.com')</pre></code-box>

二、删

-- truncate table tab_name;全表删除(可以删除约束字段,不支持数据回滚)

delete from 表 # 删除表里全部数据 delete from 表 where id=1 and name='zhangyanlin' # 删除ID =1 和name='zhangyanlin' 那一行数据

三、改

update 表 set name = 'zhangyanlin' where id>1 (一定要记得加条件,不让你就猴子他妈狒狒了!!)

DQL

四、查

select * from 表 select * from 表 where id > 1 select nid,name,gender as gg from 表 where id > 1

a、条件判断where

select * from 表 where id > 1 and name != 'aylin' and num = 12; select * from 表 where id between 5 and 16; select * from 表 where id in (11,22,33) select * from 表 where id not in (11,22,33) select * from 表 where id in (select nid from 表)

b、通配符like

select * from 表 where name like 'zhang%' # zhang开头的所有(多个字符串) select * from 表 where name like 'zhang_' # zhang开头的所有(一个字符)

c、限制limit

select * from 表 limit 5; - 前5行 select * from 表 limit 4,5; - 从第4行开始的5行 select * from 表 limit 5 offset 4 - 从第4行开始的5行

d、排序asc,desc

select * from 表 order by 列 asc - 根据 “列” 从小到大排列 select * from 表 order by 列 desc - 根据 “列” 从大到小排列 select * from 表 order by 例1 desc,列2 asc - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序

e、分组group by

select num from 表 group by num select num,nid from 表 group by num,nid select num,nid from 表 where nid > 10 group by num,nid order nid desc select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid select num from 表 group by num having max(id) > 10

特别注意:group by 必须在where之后,order by之前

猜你喜欢


二维码