–显示服务器上的数据库
mysql>show databases;
–创建一个数据库
mysql>create database m;
–创建数据库,并设置字符集
create database hive DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
–删除一个数据库
mysql>drop database m;
–选择一个数据库
mysql>use test;
–查看当前数据库中存在什么表
mysql>show tables;
–设置数据库字符集
SET character_set_client=’utf8’;
SET character_set_connection=’utf8’;
SET character_set_results=’utf8’;
–给表添加主键
mysql>alter table mytable add primary key(name);
–给表添加联合主键
Alter table cms_laucher_prodcls_item drop primary key ;
Alter table cms_laucher_prodcls_item add constraint primary key(laucher_id,prodcls_id,language_id);
–复制表
mysql>create table newtable select * from mytable;
–部分复制
mysql>create table newtable select id,name from mytable;
–查看表结构(四种方式)
mysql>describe mytable;
mysql>desc mytable;
mysql>show columns in mytable;
mysql>show columns from mytable;
–修改表结构
mysql>alter table mytable add[change,rename,drop]…要更改的内容
实例:
mysql>alter table mytable add column address varchar(80) not null;
–删除
mysql>alter table mytable drop address (restrict约束);
–修改
mysql>alter table mytable change sex sex char(2) not null;
mysql>alter table wms_interface_ip_access modify sex int
mysql>alter table students modify id int not null auto_increment;
修改字段名
alter table 表名 change column 旧字段名 新字段名 [约束条件];
–表重命名(两种方式)
mysql>rename table old_name to new_name;
mysql>alter table old_name rename new_name;
–表字段设置默认值
alter table表名alter column字段名drop default; (若本身存在默认值,则先删除)
alter table 表名 alter column 字段名 set default 默认值;(若本身不存在则可以直接设定)
****** mysql的正则表达式 **********
–找出name以a-d为开头的人的信息
mysql>select * from mytable where name regexp ‘^[a-d]’;
****** mysql的一些函数 **********
1、字符串链接—–concat()
mysql>select concat(name,’=>’,sex) from mytable;
2、数学函数
avg,sum,max,min,count
3、文本处理函数
trim,locate,upper,lower,substring
4、运算符
+ - * \
5.时间函数
date(),curtime(),day(),year(),now()….