mysql 管理
创建表create
1、 create table [if not exists] database.tablename ( 自定义内容); 例如
mysql>create table name1 (
mysql>id int unsigned auto_increment not null primary key, # 设置主键,同时自动增加
mysql>name varchar(30) not null,
mysql>age tinyint unsigned not null,
mysql>gender enum('m','f') not null default 'm' , # 指定范围和默认值
mysql>index index_score(score), #创建索引
mysql>unique(name), #创建唯一键
mysql>);
2、create table tab_name select col_name,.... from tab_name2 where ---; 从tab_name2中复制特定的内容,生成新的表
3、create table tab_name like tab_name2; 按照tab_name2的表格式创建一个内容为空的表
查询数据
表中查询数据
select
select col_name from table_name; :过滤列
where condition :过滤行
= like between...and..
!= 不等于
in 在某个特定集合中 where id in (1,2);
is null ,is not null
regexp=rlike 正则表达式
+-*/% where id + 1 > 4
and && , or || , not !
like 通配符
% 任意
_ 任意单个
limit 限制显示的行,在select 语句后边,如
select * from tab_name limit num; 显示前num行
select * from tab_name limit num1,num2; 以num1 为基础向后显示num2行
数据排序
order by clo_name
select * from test order by col_name [desc|asc]; 按照col_name中的数据排序
desc 降序 asc 升序默认,可不写
数据分组
group by cloumn 分组显示,having 组合group 使用 ,hvaing 过滤条件
select column,count(*) from table group by column hvaing >=10;
修改显示的名字 as
select column as name -----;
修改表结构
alter table table_name
1.添加新列
add col_name col_type after col_name1 ; 在现有col_name1后添加
first; 添加到第一个
2.修改字段
change old_col_name new_col_name nre-col-definition;
modify col_name col_defination; 修改表定义
3.添加索引
add {index|key} [index_name] (col_name);
删除字段
drop col_name
删除键
drop {index|key} index_name
drop primary key
重命名
rename table table_name to new_table_name
删除表
drop table [if exists] table_name
插入数据
insert into table_name (col1,col2) values ('val1','val2');
set col_name='----';
调用函数
mysql> insert into pets (name,sex,birth) values('dog','f',now());
一次插入多行
insert into table_name (name) values ('--'),('--'),('--');
replace 如果插入的主键内容已存在则覆盖
修改数据 update
updata table_name set col_name='---' [where 条件] limit 2;
删除数据
delete from table_name [shere 条件];
清空一个表 id从新开始
truncate table table_name
作者 “残雪”
bitscn.com
