MySQL数据库的基本知识

本篇博主介绍了有关MySQL的一些基础知识,

文章目录

前言

一、数据库是什么?

二、关于数据库命令

三、关于表命令

1.创建表

2.删除表:

3.查看表:

4.查看表结构:

5.更改表结构:

6.修改表约束条件(掌握添加外键)

四、数据命令

1.增加数据命令

2.删除数据命令

3.修改数据命令

五、数据命令--单表查询

1.简单查询

2.distinct去掉重复

3.算术运算符

4. 查询where 等值--where 字段名=值

5.查询where 比较 ---where 字段名>值

6.查询 where in

7.查询 where  not  in

8.查询 where like模糊查询

9.查询where or:只要满足一个条件的记录

10.查询where and:满足所有条件的记录

11.查询where between and

12.order by排序

13.group by分组查询

14.is null ,is not null

总结


前言

  来到华清远见已经一个多月了,最近我们终于学到了数据库的相关知识,今天我们一起来学习有关数据库的基本知识吧!(本人学疏才浅,如有错误,还望见谅)


一、数据库是什么?

数据库:用来存储数据,使数据永久存在。

关系数据库(SQL):

1.MySQL:开源的分布式数据库。这也是本篇博主用的哦~

2.Oracle:甲骨文。世界最大的数据库。提供完备的日志,安全处理,归档备份,恢复机制。

3.SQL Server:由Microsoft开发和推广的关系数据库管理系统。

4.SQLite:是一款轻型的数据库,常用于内存较小的嵌入式设备上。

二、关于数据库命令

1.show databases;  //查看当前mysql的所有数据库

2.create database 数据库名;  //创建数据库

3.use 数据库名;  //使用该数据库

4.drop database 数据库名;  //删除数据库

三、关于表命令

1.创建表

注意:所有符号均是英文状态下标点符号

create table 表名(

字段名 类型  <约束>,

字段名 类型  <约束>,

......

);

例:create table country(

 id int primary key auto_increme·nt,  # auto_increment自增长

 name varchar(30) unique,      #unique唯一性约束

 language char(5) not null);       #not null不为空

create database myjava1;
use myjava1;
create table country(
id int primary key auto_increment,
name varchar(10) unique,
language varchar(19) not null
);
create table president(
id int primary key auto_increment,
name varchar(10) unique,
sex char(1) not null,
f_country_id int,
constraint fk_country_id foreign key(f_country_id) references country(id)
);

2.删除表:

删除表命令:drop table 表名;

 3.查看表:

show tables;  //查看当前数据库下所有的表

 4.查看表结构:

  desc 表名; //查看该表的表结构

5.更改表结构:

(1)向表名中增加列名

alter table 表名 add column 列名 字段类型;// 

alter table student add column sex char(1);

更改表student,添加列列名是sex,类型是char(1);

(2)删除表中某一列

alter table 表名 drop column 列名;

alter table student drop column age;

更改表student,删除列,列名是age;

(3)修改表中某个列的字段类型

alter table 表名 modify column 列名 字段类型;//

alter table student modify column sex char(2);

更改表student,修改列,列名是sex,更改类型为char(2)

(4)更改表的名字

alter table 表名rename to 新表名;// to可以省略

alter table student rename to stu;

更改表student,将student表名更改为stu;

(5)更改列名

alter table 表名 change 原有列名 新的列名 新列名字段类型;

alter table stu change name xingming varchar(10);

更改表stu,将列名name改为xingming,字段类型varchar(10)

6.修改表约束条件(掌握添加外键)

表已经创建,使用alter table命令修改表结构,给表添加约束条件

alter table 表名add constraint 约束名 约束类型(字段名)

增加主键约束

alter table 表名 add constraint 约束名 primary key(列名);

alter table pre12 add constraint primary key(id);

增加唯一性约束:

 alter table person add constraint name_unique unique (name);

增加外键约束   foreign key   

alter table pre12 add constraint

foreign key(f_country_id) references country(id);

四、数据命令

1.增加数据命令

(1)插入部分列

insert into 表名(列名1,列名2,列名3.。。。)values (值1,值2,值3.。。。)

例:-- 在部门表中插入一条数据

insert into dept1( dname, loc) values('财务', '沈阳');

(2)插入所有列

insert into 表名values(值1,值2,值3.。。)

注意:值得顺序要和表定义中列的顺序一致。

2.删除数据命令

删除语法:delete from 表名 [where 条件]

3.修改数据命令

语法:update 表名

 Set 列名1=值1[, 列名2=值2,。。。。]

 [Where 条件语句]

例:-- 把7782 员工的部门编号修改为 20

UPDATE emp1  SET deptno = 20   where empno = 7782;

五、数据命令--单表查询

1.简单查询

select * from stu;  //查询stu表中所有字段段的所有数据

2.distinct去掉重复

select distinct 列1,列2 from 表名  是对整条记录去重复

3.算术运算符

例:将年龄增长1岁   select age,age+1 from stu;

4. 查询where 等值--where 字段名=值

例:select * from stu where id=29;

5.查询where 比较 ---where 字段名>值

>, <, >=, <= ,!=

例:mysql> select * from stu where age>23;

6.查询 where in

id在in()括号集合里的所有记录。

7.查询 where  not  in

例:查询年龄不等20和21的数据。age!=20  and age!=21

mysql> select * from stu where age not in(20,21);

8.查询 where like模糊查询

-- 模糊查询(like)---部分匹配

%:可以匹配0个或多个字符

_:可以匹配一个字符

例:select * from stu where age like '%3%';//查询年龄带3的记录

9.查询where or:只要满足一个条件的记录

id满足任何一个条件的记录。or等价||

10.查询where and:满足所有条件的记录

例:大于等于23岁的女生

mysql> select * from stu where sex='女' and age>=23;

11.查询where between and

例:查找年龄在25岁到35之间的记录。等价于age>=25 and age<=35;

mysql> select * from stu where age between 25 and 35;

12.order by排序

order by 列名 desc/asc;

按照某一个列进行排序,默认asc升序,desc降序,列名可以用select后面列的序号

13.group by分组查询

语法:

SELECT [column] group_function(column)  FROM table

[WHERE condition]

[GROUP BY column]

[HAVING   group_function(column)expression

[ORDER BY column|group_function(column)expression];

GROUP BY子句指定要分组的列,通过GROUP BY子句可将表中满足WHERE条件的记录按照指定的列划分成若干个小组;GROUP BY子句中可以按照多个列分组。having子句是对分组后的信息进行筛选。

14.is null ,is not null

千万不能写c_id=null,c_id!=null

例:--查询年龄为null的  select * from stu where age is null;


总结

目前博主在华清只学完了Java基础还有数据库和oracle,我们下次再见!本人学疏才浅,如有错误,还望见谅。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值