MySQL基础操作。
终端命令行,输入mysql -u 用户名 -p进去交互环境。
1、查看当前所有数据库。
show databases;
2、选定数据库,eg:选中python_data1
use python_data1;
3、显示该数据库下的所有表
show tables;
4、显示当前时间
select now();
5、查看表前10行
select * from `order` limit 10;
6、查看列数
select count(*) from `order`;
7、查看表 列的数据类型
describle `order`;
或者describle的简写desc
desc `order`;
或者
show columns from `order`;
8、查看数据当前状态【当前用户,端口等等】
status;
9、查看数据端口
show global variables like 'port';
注意port使用单引号标注的,而非引用表名的``这个标注的。
10、删除数据库中表
DROP TABLE `table_name`;
11、查看mysql的用户名
select user,authentication_string from mysql.user;
注意:高版本中的password字段,已经比authentication_string替代,但是密码加密形式体现处理,非明文。
密码忘记了,只能修改,不能找到原始的明文密码。
12、修改某个字段类型:
use sales;
alter table abnormal_od modify column city varchar(30);
13、同时修改多个字段类型:
use sales;
alter table abnormal_od change order_source order_source varchar(255),
change pay_type pay_type varchar(255),
change use_id use_id varchar(255);
14、两种方法查看某列的null值个数。isnull,ifnull函数说明链接
a,利用isnull()函数,是,返回1,否,返回0,所以用sum()求和
select sum(isnull(order_id)) from abnormal_od;
b、利用where语句,筛选是null的行,然后计算个数,用的count()函数【判断条件是 where 列名 is null】
select count(*)from abnormal_od where order_id is NULL;
15、str转date格式,利用str_to_date()函数【注意str_to_date(,format),format格式和源数据格式相同】
select str_to_date(order_date,'%Y-%m-%d') from abnormal_od;
16、mysql表中新加一列,类型date,并赋值为原始日期字段【字符串类型存在】
use sales;
alter table abnormal_od add column date_order Date;
update abnormal_od set date_order = str_to_date(order_data,'%Y-%m-%d);
注意:update 直接+表名,否则【update table +表名】报错
17、移动列的属性,要求被移动的列,标名 字段类型,否则报错。
alter table chp6_abnormal_od modify order_time time after order_id;
移到第一位
alter table chp6_abnormal_od modify city varchar(50) first;
18、删除多列
use sales;
alter table abnormal_od drop column order_data2,drop column order_date22;