【数据库系列(一)】基本操作

一、mysql服务操作

  • net start mysql                        //启动mysql服务
  • net stop mysql                         //停止mysql服务 
  • mysql -h主机地址 -u用户名 -p用户密码             //进入mysql数据库
  • quit                              //退出mysql操作
  • mysqladmin -u用户名 -p旧密码 password 新密码         //更改密码
  • grant select on 数据库.* to 用户名@登录主机 identified by “密码” //增加新用户

示例:
增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作

grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";

如果你不想test2有密码,可以再打一个命令将密码消掉。

grant select,insert,update,delete on mydb.* to test2@localhost identified by "";

二、数据库操作

  • show databases; //列出数据库
  • use database_name //使用database_name数据库
  • create database data_name //创建名为data_name的数据库
  • drop database data_name //删除一个名为data_name的数据库

示例截图:
这里写图片描述

三、数据表操作

主要是围绕“增”、“删”、“改”、”查”。

操作表
  • show tables //列出所有表
  • create table tab_name(col1 type1 [not null] [primary key], col2 type2 [not null]…) //创建一个名为tab_name的新表
    a. create table tab_new like tab_old;
    b. create table tab_new as select col1, col2… from tab_old
  • drop table tab_name 删除名为tab_name的数据表
  • describe tab_name //显示名为tab_name的表的数据结构
  • delete from tab_name //将表tab_name中的记录清空
  • select * from tab_name //显示表tab_name中的记录
  • alter table tab_name rename to new_tab_name
  • mysqldump -uUSER -pPASSWORD –no-data DATABASE TABLE > table.sql //复制表结构
操作字段
  • show columns from tab_name //同上
  • alter table tab_name add column col type
  • alter table tab_name drop col_name //在tab_name中将col_name字段删除
  • alter table tab_name modify col_name varchar(40) not null //修改字段属性,注若加上not null则要求原字段下没有数据
  • alter table tab_name change old_col new_col varchar(40); //必须为当前字段指定数据类型等属性,否则不能修改
  • alter table tab_name convert to character set utf8; //修改字符编码
视图
  • create view viewname as select statement
  • drop view viewname
表内容的基本操作

选择:select * from table1 where 范围
这里写图片描述
这里写图片描述
插入:insert into table1(field1,field2) values(value1,value2)
这里写图片描述
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ —like语法

a. % 多个字符
这里写图片描述
b. [*] 特殊字符转义 a[*]a 代表 a*a
c. _ 单字符匹配
这里写图片描述

select * from table1 where field1 regexp ‘[0-9]’ –正则表达式语法

a. “.” 匹配单个字符
b.“ * ”匹配零个或多个在它前面的东西。例如,“x*”匹配任何数量的“x”字符,“[0-9]”匹配的任何数量的数字,而“.”匹配任何数量的任何东西。”+”1个或多个 ; “?”0个或1个
c.“[aA]”匹配小写或大写的“a”而“[a-zA-Z]”匹配两种写法的任何字母。
d. 在模式开始处使用“^”或在模式的结尾用“$”
e. “{n}”“重复n次” “{n,}” 不少于n次 “{n,m}” 限定范围,m不得大于255
这里写图片描述
上例:匹配以a开头,n结束,且a后紧跟两个o,同时包含其它字母不等的字符串

排序:select * from table1 order by field1,field2 [desc]
这里写图片描述
这里写图片描述

总数:select count(field1) as totalcount from table1
这里写图片描述
求和:select sum(field1) as sumvalue from table1
这里写图片描述
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1

拷贝表: insert into b(a, b, c) select d,e,f from b;
初始化表: truncate table table1;

高级语句

对结构一样的表,合并结果:
a. UNION 运算符
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
这里写图片描述
b. EXCEPT 运算符
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
c. INTERSECT 运算符
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。

连接操作:
内连接:
指连接结果仅包含符合连接条件的行,参与连接的两个表都应该符合连接条件。

外连接:
连接结果不仅包含符合连接条件的行同时也包含自身不符合条件的行。包括左外连接、右外连接和全外连接。

左外连接【left (outer) join】::左边表数据行全部保留,右边表保留符合连接条件的行
右外连接【right (outer) join】:右边表数据行全部保留,左边表保留符合连接条件的行
全外连接【full/cross (outer) join】:左外连接 union 右外连接

示例:
数据表如下:
这里写图片描述

a.内连接:
这里写图片描述
也可以写成如下:
这里写图片描述

b. 外连接:
这里写图片描述

group by的用法:
这里写图片描述

跨表操作:
select a,b,c from a where a in (select d from b ) 或者: select a,b,c from a where a in (1,2,3)
这里写图片描述

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
这里写图片描述
剔除tab_name中存在的名字之后,查找student表中所有记录。

between的用法:
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2
视图查询:
select * from (SELECT a,b,c FROM a) T where t.a > 1;

取出从10到15的记录:
select * from (select * from table order by id asc limit 15) tab order by id desc limit 5.

四、数据的备份与恢复

导入外部数据文本:
执行外部的sql脚本当前数据库上执行: mysql < input.sql 指定数据库上执行: mysql [表名] < input.sql

数据传入命令 load data local infile “[文件名]” into table [表名];
备份数据库:(dos下)mysqldump –opt school>school.bbbmysqldump -u [user] -p [password] databasename > filename (备份)mysql -u [user] -p [password] databasename < filename (恢复)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值