例如:alter table bbs add column tel char(16) not null;
22.增加多个字段:(column可省略不写)
alter table table_name add column filed_name1 datatype,add column filed_name2 datatype;
例如:alter table bbs add column tel char(16) not null,add column address text;
23.删除一个字段:alter table table_name drop field_name;
例如:alter table bbs drop tel;
24.修改字段的数据类型:
alter table table_name modify id int unsigned;//修改列id的类型为int unsigned alter table table_name change id sid int unsigned;//修改列id的名字为sid,而且把属性修改为int unsigned
25.修改一个字段的默认值:
alter table table_name modify column_name datatype not null default "";
例如:alter table test_db modify name char(16) default not null "yourname";
26.对表重新命名:
alter table table_name rename as new_table_name;
例如:alter table bbs rename as bbs_table;
rename table old_table_name to new_table_name;
例如:rename table test_db to accp;
27.从已经有的表中复制表的结构:
create table table2 select * from table1 where 1<>1;
例如:create table test_db select * from accp where 1<>1;
28.查询时间:
select now();
29.查询当前用户:
select user();
30.查询数据库版本:
select version();
31.创建索引:
alter table table1 add index ind_id(id);
create index ind_id on table1(id);
create unique index ind_id on table1(id);//建立唯一性索引
32.删除索引:
drop index idx_id on table1;
alter table table1 drop index ind_id;
33.联合字符或者多个列(将id与":"和列name和"="连接)
select concat(id,':',name,'=') from table;
34.limit(选出10到20条)
select * from bbs order by id limit 9,10;
(从查询结果中列出第几到几条的记录)
35.增加一个管理员账号:
grant all on *.* to user@localhost identified by "password";
36.创建表是先判断表是否存在
create table if not exists students(……);
37.复制表:
create table table2 select * from table1;
例如:create table test_db select * from accp;
38.授于用户远程访问mysql的权限
grant all privileges on *.* to "root"@"%" identified by "password" with grant option;
或者是修改mysql数据库中的user表中的host字段
use mysql;
select user,host from user;
update user set host="%" where user="user_name";
39.查看当前状态
show status;
40.查看当前连接的用户
show processlist;
41.常用函数 ifnull ==(oracle, nvl)
select ifnull(b,0) from c;
42.sum(...) over( ),对所有行求和
43.sum(...) over( order by ... )
44. sum(...) over( partition by... order by ... )
45.percent_rank() over(partition by init_date,interval_type order by drawdown) 数据占的百分占