create database 数据库名

create table CeShi1
(
Uid varchar(50) primary key,
Pwd varchar(50),
Name varchar(50),
Nation varchar(50),
foreign key(Nation) references Nation(Code)
)

写查询语句需要注意:
1.创建表的时候,最后一列后面不要写逗号
2.如果有多条语句一起执行,注意在语句之间加分号分隔
3.写代码所有符号都是半角的

关系型数据库:表和表之间是有关系存在的

创建表的几个关键字:
1.主键:primary key
2.非空:not null
3.自增长列:auto_increment
4.外键关系:foreign key(列名) references 表名(列名)

CRUD操作:   增删改查

1.添加数据:
insert into Info values('','','','','') 要求values括号里面的值的个数要和表里面列数相同
insert into Info (Code,Name) values('','') 添加指定列的值

2.修改数据
update Info set Name = '张三' where Code = 'p001'

3.删除数据
delete from Info where Code = 'p001'

alter table 表名     可在已有的表中增加,删除和修改列

主键约束 primary key #一个表中只能有一个主键

alter table 表名 add primary key(列名)

唯一约束 unique #一个表中可以有多个unique

alter table 表名 add unique(列名)

外键约束 foreign key ···references #一个表中可以有多个外键

altre table 外键表名 add foreign key (列名) references 主键表名(列名)

查询数据:

1.普通查询,查所有的
select * from Info #查所有数据
select Code,Name from Info #查指定列

2.条件查询
select * from Info where Code = 'p001' #一个条件
select * from Info where Name = '张三' and Nation = 'n001' #两个条件并的关系
select * from Info where Name = '张三' or Nation = 'n001' #两个条件或的关系

3.排序查询
select * from Info order by Birthday #默认升序排列asc 如果要降序排列 desc
select * from Car order by Brand,Oil desc #多列排序

4.聚合函数
select count(*) from Info #取个数
select sum(Price) from Car #查询price列的和
select avg(Price) from Car #查询price列的平均值
select max(Price) from Car #查询price列的最大值
select min(Price) from Car #查询price列的最小值

5.分页查询
select * from Car limit n,m #跳过n条数据取m条数据
select * from Car limit (n-1)*m,m #第二页跳过(n-1)*m条数据取m条数据

6.分组查询
select Brand from Car group by Brand #简单分组查询
select Brand from Car group by Brand having count(*)>2 #查询系列里面车的数量大于2的系列

7.去重查询
select distinct Brand from Car

8.修改列名
select Brand as '系列' from Car

9.模糊查询
select * from Car where Name like '_迪%'       %代表任意多个字符 _代表一个字符

10.离散查询
select * from Car where Code in ('c001','c002','c003','c004')
select * from Car where Code not in ('c001','c002','c003','c004')

高级查询:

1.连接查询

select * from Info,Nation #得出的结果称为笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code

join on连接

select * from Info join Nation #join连接
select * from Info join Nation on Info.Nation = Nation.Code

2.联合查询

select Code,Name from Info
union
select Code,Name from Nation

3.子查询

1)无关子查询

select Code from Nation where Name = '汉族' #去Nation表中查询汉族的民族代号

select * from Info where Nation = (民族代号) #在Info表中查询民族代号为上一个查询结果的所有信息

select * from Info where Nation = (select Code from Nation where Name = '汉族')

子查询查询的结果被父查询使用,子查询可以单独执行的称为无关子查询

2)相关子查询

select * from Car where Oil<(该系列的平均油耗) #查询油耗小于该系列平均油耗的

select avg(Oil) from Car where Brand = "值" #查询某系列的平均油耗

select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)    #a, b为别名

#内层查询时外层查询是固定的: 查询时按顺序一条一条查询

select avg(Oil) from Car b where b.Brond = 'b001' #结果为8.7

#外层查询就变为:

select * from Car where Oil < 8.7

MySQL常用代码的更多相关文章

  1. Mysql:常用代码

    C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...

  2. Php mysql 常用代码、CURD操作以及简单查询

    C/S:Client ServerB/S:Brower Server php主要实现B/S LAMP :Linux系统    A阿帕奇服务器    Mysql数据库   Php语言 mysql常用代码 ...

  3. Mysql基础代码(不断完善中)

    Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...

  4. MySQL数据库3 - MySQL常用数据类型

    一. MySql常用数据类型 数据类型:整数(tinyint smailint int bigint) 定点数 decimal(p,s) ------ 小数点位置固定的       ---> 数 ...

  5. mysql常用函数参考

    mysql常用函数参考   对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL, ...

  6. PHP常用代码大全(新手入门必备)

    PHP常用代码大全(新手入门必备),都是一些开发中常用的基础.需要的朋友可以参考下.   1.连接MYSQL数据库代码 <?php $connec=mysql_connect("loc ...

  7. 学生选课数据库SQL语句45道练习题整理及mysql常用函数(20161019)

    学生选课数据库SQL语句45道练习题: 一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四 ...

  8. MySQL常用的七种表类型(转)

    MySQL常用的七种表类型(转)   其实MySQL提供的表类型截至到今天已经有13种,各有各的好处,但是民间流传的常用的应该是7种,如果再细化出来,基本上就只有两种:InnoDB.MyIASM两种. ...

  9. phpcms v9模板制作常用代码集合(转)

    phpcms v9模板制作常用代码集合(个人收藏) 1.截取调用标题长度 {str_cut($r[title],36,'')} 2.格式化时间 调用格式化时间 2011-05-06 11:22:33 ...

随机推荐

  1. poj 1390 Blocks (经典区间dp 方块消除)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4250   Accepted: 1704 Descriptio ...

  2. discuz !NT 3.5 论坛整合 .net 网站用户登录,退出

    using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlCont ...

  3. 【Cocosd2d-x CCMenu菜单之二】

    菜单项CCMenuItem是一个基类. 子类CCMenuItemFont.CCMenuItemLabel.CCMenuItemSprite.CCMenuItemToggle可增加CCMenu中形成菜单 ...

  4. sessionStorage / localStorage

    var referurl = document.referrer; //上级网址 if(referurl.indexOf('address_order')>0){ //判断是否是从上一级地址跳转 ...

  5. 匿名函数 invoke

    delegate string MyDele(string str); string MyFun(string str) { return str; } private void Form1_Load ...

  6. javascript 温故而知新 getBoundingClientRect

    getBoundingClientRect获取元素位置  getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置. getBoundingClient ...

  7. mysql时间操作(时间差和时间戳和时间字符串的互转)

    mysql时间操作(时间差和时间戳和时间字符串的互转) 两个时间差: MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数. select dat ...

  8. C++中没有定义类的引用。

    在有时候由于类太大.须要在类在后面定义: 比如: class Y{ void f(X); }; class X{ //一些成员数据和函数 }; //error 由于c++要求不论什么一个变量在引用之前 ...

  9. js 正则匹配 域名【host】

    如果直接在js中是可以直接取到hostname的,以下方式是通过正则匹配: var url = "http://www.cnblogs.com/cench" var reg = / ...

  10. 自定义type='file'上传文件样式

    改变默认的上传文件样式: 用label作为替代 <input id="file_-1" type="file" name="file" ...