常用命令:
设置密码:
格式:mysql> set password for 用户名@localhost = password('新密码');
例子:mysql> set password for root@localhost = password('123');
linux 启动、停止、重启mysql:
systemctl start mysql
systemctl stop mysql
systemctl restart mysql
service mysqld start
service mysqld stop
service mysqld restart
7月2日:
sql基础命令:
select
select * from table_name;
select distinct
select distinct title_value from table_name;
where
select * from table_name where 条件;
and or
select * from table_name where 条件 and/or 条件;
order by
select * from table_name order by title;
insert into
insert into table_name values (value, value, ...) (value, value, ...);
update
update table_name set title=value where 条件;
7月3日:
建表时选项:
1、create table选项
1、指定列选项:default、comment
2、指定表选项:engine、auto_increment、comment
2、create table约束
1、not null:非空约束
2、unique:唯一约束
3、primary key:主键约束
4、foreign key:外键
5、check:检查---enum、set
例:
create table websites (id int primary key auto_increment, name varchar(20) not null) charset=utf8;
limt
select * from table_name limt 2;
like
select * from table_name where name like '%A%';
in
select * from table_name where name in (value1, value2);
between
select * from table_name where num between 10 and 20;
as
select w.name, w.url, a.count, a.date from websites as w, access_log as a where a.site_id=w.id;
7月4日:
join 连接表
inner join 返回左右表同时匹配的行(交集)
left join 以左表行为准,左表行未匹配右表中的行,仍返回该行
right join 以右表行为准,右表行未匹配左表中的行,仍返回该行
例:
select w.name, w.url, a.count, a.date from websites as w left/right/inner join access_log as a on w.id=a.site_id;
union 合并不同的查询结果,保证列数相同、列数据类型相似
union all 返回重复项
select into 复制旧表内容至新表
create table new_table select * from old_table;(MySQL)
insert into select 复制旧表内容插入至存在的表
foreign key 外键,指向另一表的主键,相对应
例:
create table boss ( id int not null auto_increment, company_name varchar(20) not null, name varchar(20) , site_id int ,primary key (id), foreign key (site_id) references websites(id)) charset=utf8;
7月5日:
check 约束(MySQL中存在问题)
alter table table_name add constraint check_name check (id>0);
index 索引
create index index_name on table_name (column_name);
drop
删除索引 alter table table_name drop index index_name;
删除数据库 drop database db_name;
删除表 drop table table_name;
truncate 仅删除表内数据
truncate table table_name;
view 视图
动态查询集,定义好的查询条件
创建视图:
create view view_name as select ...
视图查询:
select * from view
视图更新:
create or replace view view_name as select ...
删除视图:
drop view view_name
7月6日:
公司开会到10点
7月7日:
mysql事务:
begin 开始事务
rollback 回滚
commit 提交
中间可以添加保存点、删除保存点、回滚到保存点
中途遇到的问题(待解决):
1.使用 insert into select 复制表数据到存在的表时,复制的行id变为0,且再插入数据行id也为0
insert into websites_bac (name, url, alexa, country) select name, url, alexa, country from websites;
2.为表中行增加check后,约束不起作用,仍能添加不符合约束的行,但id变为0,且再插入数据行id也为0
3.导出数据失败
select * from websites into outfile '/home/tanfeng/data.txt';
报错:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
查资料说修改配置文件,尝试依然失败,待解决