参数了解
参数 | 描述 |
---|---|
-u | 用户名 |
-h | 地址 |
-p | 密码 |
MySQL基本命令
通过mysql -u root -p 来启动mysql
show databases ; #查看当前实例下包含多少个数据库
create database [IF NOT EXISTS] 数据库名 ; # 创建新的数据库
drop database 数据库名 ; #删除指定数据库
use 数据库名 ; #进入指定数据库
show tables ; #进入数据库后查询该数据库下包含多少个数据表
desc 表名 ;# 查看指定数据表的表结构
show create table 表名;#观看建表语言
创建和使用数据库
数据库大小写敏感,而SQL语句不敏感
create database menagerie;
use menagerie # 进入数据库
mysql -h host -u user -p menagerie # 连接到指定数据库,不然每次都要使用use来进入
创建表
show tables; # 查看所有表
create table student (name varchar(20), sex char(1), birth date, death date); # 创建表
describe student; # 查看表
导入数据
load data local infile ‘/path/student.txt’ into table student; # 将txt文件导入数据表
获取数据
select *from student;#获取数据
查询指定行
select *from student where name=“…”;#查询指定行
查询指定列
select name from student;#查询指定列
指定列排序
select name ,birth from student order by birth desc;#指定列排序
模式匹配
1.like
select *from student where name like ‘b%’;以b开头的名字
select *from student where name like ‘%fy’; 以fy结尾的
select *from student where name like ‘%w%’; 包含w的
select *from student where name like ‘_____’; 姓名长度为5的 一个_代表一个字符
2.regexp_like ()
select *from student where regexp_like (name,’^b’);以b开头的
select *from student where regexp_like (name,‘fy$’); 以fy结尾的
select *from student where regexp_like (name,‘w’);包含w的
select *from student where regexp_like (name, ‘^…$’); 长度为5的
sql语法
定义语句:
---------------- 数据库 ---------------表-------------视图------------索引
创建 —create database–create table–create view–create index
删除-----drop database----drop table-----drop view----drop index
修改-----alter database-----alter table-----------------------------------
1.插入列
alter table 表名 (table_name)列名(column_name)类型
2.添加约束
alter table 表名 add check(列名>=100)#数值约束
3.删除约束
alter table 表名 drop constraint 约束名
4.删除列
alter table 表名 drop column 列名
5.建索引
create index 索引名 on
基础阶段
1.select
select 列名 from 表名
select * from Student 显示Student所有信息
2.distinct
select distinct 列名 from 表名 显示表中唯一的列名(去除重复)
3.where
select * from Student where name=‘张三’ 条件选择
where里还能添加 between 、=、!=、>、<、like 、and、or等
select * from Student where age>20
4.and和or 把两个或者多个条件连接起来
select * from Student where name=‘张三’ and age=20
select * from Student where name=‘张三’ or age=20
5.order by
用来排序的,可以根据字母排序也可根据数字排序,有升序和降序两种 默认为升序,降序加上desc
select * from Student order by age
6.insert into
插入语句。
insert into student(name,age) values (‘李四’,21)
7.update
update用于更新数据
update Student set age=22 where name=‘张三’
8.delete
删除表中的行
delete from Student where name=‘李四’
9.创建数据库
create database Student
11.约束
创建表的时候还应该对表添加一些约束,例如:主键,是否为空之类的
create table Student
(
id int not null auto-creament,
name varchar(255) not null,
age int ,
primary key(id)
)
12.删除数据库或表
droptable Student
drop database Student
清空表中的数据
truncate table Student
没有删除表,只是清空了数据而已
Alter table Student
add birth date
Alter 是用来改变表或数据库的关键字
进阶阶段
1.top用来规定返回的记录的数目
select top 1 * from Student 返回Student表的第一条数据
select top 50 percent * from Student 返回Student表50%的数据
2.like用来指定模式匹配
select * from Student where name like ‘张%’ 返回Student表里名字以张开头的数据
这里介绍一下通配符
% 代表一个或者多个字符
_ 代表一个字符
[abc] abc中任一字符(这里类似java的regex)
[^abc] 或者 [!abc] 不在abc中的任意字符(1个)
href=“http://3.in”>http://3.in
允许在where里规定多个值
select * from Student where name in (‘张三’,‘李四’)
4.between…and操作符选取了一个范围
select * from Student where age between 15 and 30 选取15到30之前包含15的(mysql)
不同数据库对这个包含的含义不同
5.Alias用于表的别名
select name as n , age as a from Student
6.join…on连接2个或者多个表
连接两个表需要注意,其中一个表中必须有另外一个表的主键,根据这个主键来连接。
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons INNER JOIN Orders ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName
inner join = join 选取两张表中共同的部分
left join 选择左边表中所有部分
right join 选择右边表中所有部分
full join 选择两张表中所有部分
7.Union合并两张表
前提:两张表有相同数量的列,列的数据类型也必须相似
8.select into从一个表里选择数据插入到另外一个表里
select * into Student_backup from Student