安装下载
第一种
安装mysql安装包 //www.jb51.net/softs/451120.html
保存root密码
打开系统偏好设置,start mysql server
-
#配置mysql
export Mysql=/usr/local/mysql
export PATH=$PATH:$Mysql/bin -
重置root密码
mysqladmin -u root -p password newpass
-
登录
mysql -u root -p
-
退出
exit
第二种
-
安装mysql
brew install mysql
-
初始化mysql,然后按提示操作就OK
mysql_secure_installation
-
登录
mysql -u root -p
-
退出
exit
mysql数据库操作
命令 | 注释 |
---|---|
show databases | 展示所有数据库名称 |
use databasename | 针对该数据库进行相应操作 |
show tables | 展示该数据库所有表 |
show columns from tablename | 展示该表的所有属性,属性类型,主键信息,是否为NULL,默认信息等 |
show index from tablename | 展示该表的详细索引信息,包括PRIMARY KEY主键 |
create database databasename | 创建数据库 |
drop database databasename | 删除数据库 |
mysql表操作
-
创建表
create table [if not exist] 'tablename'(
'colname1' int not null auto_increment,
'colsname2' varchar(20) not null,
'colsname3' char(4),
primary key(colname1,colname2),
foreign key(colname2)
)engine = InnoDB default charset =utf8; -
删除表
drop table[if exist]'tablename'[restrict | cascade]
约束条件:
1)restrict(默认):如果存在依赖该表的对象,则此表不能删除。
2)cascade:该表删除时没有限制条件,在删除基本表的同时,相关的依赖对象都将一起删除 -
更改表结构
alter table 表名 add 列表 列数据类型 [after 插入位置];
alter table 表名 change 列名称 列新名称 新数据类型;
alter table 表名 drop 列名称; -
增加列
insert into table(列1,列2,列3...) value(值1,值2,值3...)(值1,值2,值3...)(值1,值2,值3...)...
-
删除行
delete from table where
-
更新表信息
update table set alary=salary+1 where 按条件更新数据
-
查询
select [distinct] value as new_vallue
from table new_table
where ___
group by _____
having _____
order by ____ desc / asc
limit _ offset _ 左连接: left join tabla_从 on #以主表为主
右连接: right join tabla_从 on #以从表为主
内连接: inner join tabla_从 on #以主从表交集为主
全连接: full join tabla_从 on #以主从表并集为主(mysql目前不支持此种方式) 交集: select no from table_1 intersect select no from table_2 #1交2
并集: select no from table_1 union select no from table_2 #1并2
差集: select no from table_1 except select no from table_2 #1 - 2 运算符:=, <=>, <>, !=, <=, <, >=, >, !, &&, ||, in (not) null, (not) like, (not) in, (not) between and, is (not), and, or, not, xor is/is not 加上ture/false/unknown,检验某个值的真假 <=>与<>功能相同,<=>可用于null比较 排序子句: 升序:ASC,降序:DESC 去重子句: select distinct salary Limit子句:可以被用于强制 SELECT 语句返回指定的记录数。Limit接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。
//初始记录行的偏移量是 0(而不是 1):
mysql> SELECT * FROM table LIMIT 5,10; #检索记录行6-15
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
mysql> SELECT * FROM table LIMIT 95,-1; #检索记录行 96-last
//如果只给定一个参数,它表示返回最大的记录行数目。换句话说,LIMIT n 等价于 LIMIT 0,n:
mysql> SELECT * FROM table LIMIT 5; #检索前 5 个记录行
//offset子句,跳过前多少条,在选择第几条:
select * from employees order by hire_date desc limit 1 offset 2; #跳过前两条检索,实际检索第三个个记录行