sql 语句学习 1 -持续更新中

1.连接数据库

  mysql -u用户名 -p密码
   mysql - uroot -pli
Screen Shot 2016-05-15 at 3.43.37 PM.png

2.展示数据库

show databases;
Screen Shot 2016-05-15 at 3.45.16 PM.png

3.操作数据库

 use user;
Screen Shot 2016-05-15 at 3.46.04 PM.png

4.展示数据库中的表

show tables;
Screen Shot 2016-05-15 at 3.48.16 PM.png

5.结构化查询语言-sql(structured query language)

1.DDL 数据定义语言

create:创建表
//table_name 表名 //id 主键 类型 int 自增长 //name 字段名 类型 varchar(20) //introduce 字段名 类型 text create table table_name ( id int unsigned auto_increment primary key, name varchar(20), introduce text); //主键索引和普通索引 create table t1( id int unsigned auto_increment ,name,primary key(id),index in_name(name)); //添加索引的目的是加快查询速度
drop:删除表
drop table table_name;
alter://修改表
//给已有的表添加字段 alter table table_name add age int; //删除字段 alter table table_name drop age; //修改表中已有的字段的属性 alter table table_name modify age int not null default 20; //修改表中已有的字段的名字 alter table table_name chanage age user_age int; //查看表中的索引 show index from table_name; show index from table_name\\G //修改表名 alter table table_old_name rename table_new_name; //给字段添加索引 alter table table_name index in_name(name); //删除表中索引 alter table table_name drop index in_name; //打印sql语句执行信息 desc sql语句;

2.DML 数据操作语言

insert:增
insert into table_name(字段名) values('字段值'); insert into table_name(字段名1,字段名2) values('字段值1','字段值2');
delete:删
//删除talbe_name中id=6的记录 delete from table_name where id = 6; //删除talbe_name中id=6或者id=7的记录 delete from table_name where id = 6 or id = 7; delete from table_name where id in (6,7); //删除表中id>6&&id<8 delete from table_name where id > 6 and id < 8; delete from table_name where id between 6 and 8;
update:改
//修改表中id等于6的name等于a update table_name set name = 'a' where id = 6; //修改表中id等于6的name等于a,pass等于lsb update table_name set name = 'a' ,pass = 'lsb' where id = 6;

3.DQL 数据查询语言

select:查
//查询所有字段 select * from table_name; //查询特定字段 select id from table_name; //查询特定字段然后取别名 select id uid from table-name; //取唯一值 select distinct from table_name; //查询字段是否为空 select * from table_name where pass is null; //查询地段不为空的 select * from table_name where pass i not null; //查询字段中包含c的 select * from table_name where name like '%c%'; select * from table_name where name regexp '.*c.*'; //查询字段中包含c或者包含b的 select * from table_name where name like '%c%' or name like '%b%'; select * from table_name where name regexp '(.*c.*)|(.*b.*)'; //排序 默认升序 续写或者写 asc select * from table_name order by id; select * from table_name order by id asc; //id字段按降序排 select * from table_name order by id desc; //查询id前三大的记录 select * from table_name order by id desc limit 3; select * from table_name order by id desc limit 0,3; //mysql 字符串连接符 select concat('a','b'); select concat(id,name) from table_name; //随机排序 select * from table_name order by rand(); //随机排序取前五个 select * from table_name order by rand() limit 5; select * from table_name order by rand() limit 0,5; //统计 select count(*) from table_name; select count(id) from table_name; select count(id) total from table_name;//取别名 //求和 sum select sum(id) from table_name; //求平均值 select avg(id) from table_name; //求最大值 select max(id) from table_name; //求最小值 select min(id) from table_name; //取最大值和最小值 select max(id) max_value,min(id) min_value from table_name; //分组聚合 //按table_name表中的name分组 select name ,count(id) from table_name group by name; //给分组取别名 select name,count(id) total from table_name group by name; //分组降序排列 select name ,count(id) total from table_name group by name order by total desc; //查询分组中大于5条的 select name ,count(id) total from table_name group by name having total > 5;

4.DCL 数据控制语言

数据控制语言(DCL)是用来设置或者更改数据库用户或角色权限的语句,这些语句包括GRANT、DENY、REVOKE等语句,在默认状态下,只有sysadmin、dbcreator、db_owner或db_securityadmin等角色的成员才有权利执行数据控制语言。

6.多表查询

1.普通多表查询
//查询t1中name和t2中uid相等的记录 select * from t1,t2 where t1.name = t2.uid; //分组查询 select t.name,t2.uid from t1,t2 where t1.name = t2.uid groupd by t2.uid
2.左连接多表查询
select t1.name ,t2.uid from t1 left join t2 on t1.name = t2.uid;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,392评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,258评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,417评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,992评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,930评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,199评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,652评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,327评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,463评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,382评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,432评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,118评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,704评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,787评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,999评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,476评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,057评论 2 341

推荐阅读更多精彩内容