mysql基础

mysql基础

最近工作的原因得去弄数据库,之前是学过简单的增删改查,但早已忘光了,现在重新系统的学下mysql,可以在工作中运用。

壹、数据库简介

数据库无处不在,那什么是数据库?根据定义,数据库只是一个结构化的数据集合,数据本质上相互关联,列如产品属于产品类别并与多个标签相关联。这就是为什么要使用关联数据库,在关系数据库中,我们使用表对产品,类别,标签等数据进行建模。表包含列和行。它就像一个Excel,表可以涉及的使用有:一对一,一对多,多对一等关系,因为我们要处理大量的数据,所以需要一种方法来定义数据库,表等,并更有效地处理数据。另外,我们可以将数据转换成数据信息。

所以就需要SQL来处理了。

 linux安装方法:https://blog.csdn.net/qq_15766181/article/details/51962804

一、mysql基本命令操作
1、简单的增插删查改
1)创建、查看、选择数据库
创建一个数据库wdy_test
create database wdy_test
创建时通过character set gbk 将数据库字符编码指定为gbk
create database wdy_test character set gbk;
查看数据库
show databases;
选择数据库
use database wdy_test
2)创建数据库表
格式 create table 表名称(列声明); #备注:列的名称以及该列的数据类型将在括号内完成;
例子
create table table_test
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
#备注:括号内声明了5列内容,id name sex ahe tel为每列的名称,后面跟的是数据类型描述,列与列的描述之间用逗号隔开。
以id int unsigned not null auto_increment primary key进行解释;
id为列的名称,int指定该列的类型为int(取值范围为-8388608到8388607);
unsigned表示该类型为无符号型,取值范围为0-16777215;
not null 说明该列的值不能为空,如果不指定该属性的话默认为空;
auto_increment需要在整数列中使用,其作用是在插入数据时若该列为null,mysql将自动产生一个比现存值更大的唯一标识符值,在每张表中仅能有一个这样的值且所在列必
须为索引列。
primary key" 表示该列是表的主键, 本列的值必须唯一, MySQL将自动索引该列。
3)向表中插入数据
insert 语句可以用来将一行或多行数据插到数据库表中
格式 insert [into] 表名 [(列名1,列名2,列名3,...)] values (值1,值2,值3,....);
其中[]内的内容是可选的,例子
insert into table_test values (null,"wudongyu","man",29,110);
有时我们只需插入部分数据,或者不按顺序来操作,可以通过列名来操作
insert into table_test(name,sex)values("wudongyu","man");
4)查看表中的数据
select语句常用来根据一定的查询规则到数据库中获取数据
语法 select 列名称 from 表名称 [查询条件];
例子,查刚刚创建的表
select * from table_test; #备注:查所有
select name,age from table_test;#备注:查name和age
按特定条件查询:
where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;
例子 select * from table_test where sex="man";
where子句不仅仅支持wehere 列名="值"这种名等于值的查询形式,对一般的比较运算符都是支持的 =、>、<、>=、<、!=
以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询。
例子
select * from table_test where age >18; #备注:查询年龄在18岁以上的所有人信息
select * from table_test where name like "%wu%"; #备注:查询name里面有带wu的所有人信息
select * from table_test wehre id<5 and id>2 and age>18; #备注:查询id小于大于2小于5且年龄大于18的所有人信息。
5)更新表中的数据
update语句可用来修改标志牌楠哥的数据
语法: update 表名称 set 列名称=新值 where 更新条件;
例子: update table_test set age=old where id=2; #备注:
update table_test set old=age+4; #备注:所有人的old都加增加4岁
update table_test set name="期待某一天的小男孩",old="18" where tel=110; #备注:将手机为110的人的姓名改为期待某一天的小男孩,岁数改成18
6)删除表中的数据
delete语句用于删除表中的数据
语法 delete from 表名称 wehere 删除条件;
例子
delete from table_test where id=5; #备注:删除table_test表里面id=2的行
delete from table_test where od<4; #备注:删除table_test表里面id>4的行
delete from table_test; #备注:删除表的所有数据
7)创建后表的修改
alter table语句用于创建后对表的修改
语法:
添加列 alter table 表名 add列名 列数据类型 [after 插入位置];
例子 alter table table_test add address char(60); #备注 在最后列添加个address
alter table table_test add wage date after age; #备注 在age的列后面插入wage
修改列 alter table 表名 change列名称 列新名称 新数据类型;
例子 alter table table_test change age old char(20) default "-";#备注 将age改成old
alter table table_test change name name char(32) not null;
删除列 alter table 表名 drop 列名称;
alter table table_test drop wage; #备注 删除wage列
重命名表
alter table table_test rename test01;
删除整张表
drop table test01;
删除整个数据库
drop database test;

 

原文地址:https://www.cnblogs.com/wudongyu/p/8870383.html