MySQL简明入门教程

来源:互联网 发布:淘宝的用户画像 编辑:程序博客网 时间:2024/05/29 08:51

声明:sql关键字不区分大小写,注释使用--

  • 创建数据库
-- 创建mt_test这个数据库create datebase mt_test;
  • 选择数据库
-- 选择使用数据库,意思是后面的操作的数据库表默认使用该数据库use mt_test;
  • 删除数据库
-- 删除数据库drop datebase mt_test;
  • 建表
-- 创建表,f_id是自增字段,f_id也是主键,要求插入的每条数据的f_id不能重CREATE TABLE t_log (  f_id bigint(20) NOT NULL AUTO_INCREMENT,  f_opt_type bigint(20) NOT NULL,  f_opt_content varchar(256) DEFAULT '0',  f_sku_id bigint(20) DEFAULT '0',  f_item_id bigint(20) DEFAULT '0',  f_distributor_id bigint(20) DEFAULT '0',  f_supplier_id bigint(20) DEFAULT '0',  f_seller_id bigint(20) DEFAULT '0',  f_spec varchar(256) DEFAULT NULL,  f_sale_price bigint(20) DEFAULT '0',  f_wd_price bigint(20) DEFAULT '0',  f_ratio bigint(20) DEFAULT '0',  f_create_time datetime DEFAULT NULL,  f_supplier_sku_id bigint(20) DEFAULT '0',  f_dis_sku_id bigint(20) DEFAULT '0',  f_platform_ratio bigint(20) DEFAULT '0',  f_dis_shop_id bigint(20) DEFAULT '0',  f_dis_status bigint(20) NOT NULL DEFAULT '0',  f_supplier_status bigint(20) NOT NULL DEFAULT '0',  f_sup_dis_status bigint(20) NOT NULL DEFAULT '0',  f_dis_seller_status bigint(20) NOT NULL DEFAULT '0',  PRIMARY KEY (f_id)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  • 查看表详情
-- 查看表字段详情desc t_log;-- 查看建表语句show create table t_log;
  • 显示数据库的表
-- 查看数据库包含的数据库表列表show tables;
  • 删除表
-- 删除表,包括表里面的数据全部删了drop table t_log;
-- 表插入数据-- 通过key=value的形式可以按照任意顺序给字段赋值,也可以省去不必要的字段赋值,而且看起来很清晰,建议代码中都使用这种方式insert into t_log set f_opt_type=1,f_opt_content='opt_context内容部分',f_sku_id=1,f_item_id=2,f_create_time=now();
-- 表删除数据delete from t_log where f_id=11;
-- 表更新数据,一般要求加where语句,表明要更新的记录条件,否则会更新整个表,很可怕的update t_log set f_sku_id=11 where f_id=1;
-- 表查询数据,要查询的字段一一列出,不要使用*select f_id,f_create_time from t_log;-- 选取所有字段的数据,不要使用*,这里使用where加了选择条件select f_id,f_opt_type,f_opt_content,f_sku_id,f_item_id,f_distributor_id,f_supplier_id,f_seller_id,f_spec,f_sale_price,f_wd_price,f_ratio,f_create_time,f_supplier_sku_id,f_dis_sku_id,f_platform_ratio,f_dis_shop_id,f_dis_status,f_supplier_status,f_sup_dis_status,f_dis_seller_status from t_log where f_id>10;-- and使用,表示要同时具备select f_id,f_create_time from t_log where f_id=3 and f_create_time<'2015-06-04 00:00:00';-- or使用,只要满足其一就可以select f_id,f_create_time from t_log where (f_id=3 or f_id=4) and f_create_time<'2015-06-04 00:00:00';-- 计数count(1)统计记录数select count(1) from t_log where f_id<100;-- 对结果排序,asc升序,desc降序select f_id,f_create_time from t_log ordey by f_id asc;-- 连表查询,查询用户的uid,uin,mobile,由于uid在uid表,所以需要连表,两个表中一个用户的uin相同,-- 所以可以用这个字段来连表,此外由于表名太长,这里用了表名别称select a.f_uin,a.f_mobile,b.f_uid from t_user_41 a,t_uid_user_map_40 b where a.f_uin=b.f_uin;
1 0