mysql -u root -p[pwd]
\q
create database testdb;
show database;
drop database testdb;
use testdb;
create table if not exists table_name (
id int UNSIGNED AUTO_INCREMENT,
name varchar(50),
userId int,
foreign key(userId) references table_name2(userId)
);
show tables;
desc table_name;
drop table table_name;
insert into table_name(id, name) values(1, ‘dkangel’);
select id, name from table_name where name=‘xx’ and id=1;
update table_name set id=2, name=‘xx’ where name=‘yy’;
delete from table_name where id=1;
select * from table_name where name like ‘%xx%’;
select name from table1 where conditions
union / union all(包含重复数据)
select name from table2 where conditions;
select id, name from table_name order by id asc(desc), name asc(默认升序);
select name, count(*) as count from table_name group by name with rollup(再统计);
inner join 等值连接,公共部分
select a.name, b.age from table1 a inner join table2 b on a.id = b.id;
相当于:select a.name, b.age from table1 a, table2 b where a.id = b.id;
left join 已左边为主
select a.name, b.telephone from table1 a left join table2 b on a.id = b.id;
right join 已右边为主
select a.name, b.telephone from table1 a right join table2 b on a.id = b.id;
select * from table1 where telephone is null (is not null);
select * from table1 where name regexp ‘^dkangel’
ACID
desc table_name;
show columns from user;
字段
alter table table_name add addr varchar(50);
alter table table_name drop addr;
alter table table_name modify addr varchar(100);
默认值
alter table table_name alter addr set default ‘xx’;
alter table _table_name alter addr drop default;
表名
alter table table_name rename to table_name_new;
一般应用在SQL查询语句的条件(一般作为where子句的条件)
适用场景:查多改少,索引存在索引表里,该表保存了主键和索引字段并指向实体表的记录,更新索引字段时需要同步更新索引表中的数据
创建索引(唯一索引unique)
create unique index index_name on table_name(column1, column2);
alter table table_name add unique index_name(c1);
create table table_name(name varchar(50), unique index index_name(name));
查询索引
show index on table_name;
删除索引
drop index index_name from table_name;
alter table table_name drop index index_name;
create temporary table table_name(…);
获取数据表的完整结构
auto_increment
唯一约束
primary key
UNIQUE
(column1)distinct
col1, col2 from table_name;select max(column_name) from table_name;
select min(column_name) from table_name;
select avg(column_name) from table_name;
select * from table_name into outfile ‘D:\xx
\data.txt’;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
show global variables like ‘%secure_file_priv%’;
secure_file_priv=’’
secure_file_priv
用于限制LOAD DATA, SELECT …OUTFILE, LOAD_FILE()传到哪个指定目录
mysqldump -u root -p database_name table_name > ‘tmp.txt’
mysql -u用户名 -p密码 < 要导入的数据库数据(runoob.sql)
mysql> create database abc;
mysql> use abc;
mysql> set names utf8; # 设置编码
mysql> source /home/abc/abc.sql # 导入备份数据库
load data local infile ‘tmp.txt’ into table table_name(a, b, c);
mysqlimport -u root -p --local database_name dump.txt
算数运算符
+
-
*
/
div 商
mod 取余
比较运算符
=
!=
<
>
<=
>=
between
not between
in
not in
like
regexp
is null
is not null
逻辑运算符
not
and
or
xor 异或
位运算符
& 按位与
| 按位或
^ 按位异或
! 取反
<<左移
>> 右移