SQL数据库语言基础
表的创建:
1.创建列(字段):列名+类型
2.设置主键列:能够唯一标识一条数据
3.设置唯一:内容不能重复
4.外键关系:
一张表(从表)其中的某列引用自另外一张表(主表)中的主键列
设计表:
数据库的三大范式:
1.第一范式:(每一列的原子性)
每一列在某个程序中是不可拆分的最小原子。
2.第二范式:(每一列都要和主键列有关)
3.第三范式:(每一列都要和主键有直接关系)
FK外键 PK主键
T-SQL语句:
创建数据库:create database mytest
使用数据库(切换数据库):use 数据库库名
数据库:程序用来存取数据的。
ACCESS,SQL SERVER,MYSQL,Oracle
数据库:服务、界面
数据库在存储数据的时候,也是使用的表格方式:
列(字段):
行(记录):
创建数据库:
主文件:有且只有一个 .mdf
日志文件:
次数据文件:.ndf
数据库的附加和分离
分离:找到数据库,点击右键--任务--分离,弹出对话框,点确定
分离完之后,可以复制数据库的文件到其它电脑 D:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
附加:选中数据库,右键--附加--弹出对话框--选择附加的文件--点击添加找到要附加的文件--确定
数据库的备份和还原
1.创建数据库:create database 数据库名称
2.使用数据库:use 数据库名称
3.创建数据表:
create table RenYuan
(
code varchar(50) primary key,
name varchar(50) unique,
sex bit,
nation varchar(50) not null references MinZu(code),
birthday datetime
)
create table MinZu
(
code varchar(50) primary key,
name varchar(50)
)
create table Friends
(
ids int identity primary key,
mcode varchar(50),
fcode varchar(50)
)
自增长列:
go:如果多条语句要一起执行,那么在每条语句之后需要加go关键字
创建有外键表的时候,要先创建主表,再创建从表
关键字:
primary key 主键
unique 唯一键
not null 非空
references 外键关系(引用)
identity 自增长
ids int identity primary key,
identity 自增长
primary key 主键
unique 唯一键
not null 非空
references 外键(引用)
1.删除表
drop table Student
2.修改表
alter table RenYuan add CC int
alter table RenYuan drop column CC
3.删除数据库
drop database CeShi
CRUD操作
create 添加数据 read 读取数据 update 修改数据 delete 删除数据
1.添加数据
insert into Nation values('n002','回族')
insert into Nation values('n003','')
insert into Nation(code,name) values('n004','维吾尔族')
insert into Friends values('p001','p007')
2.删除数据
delete from Nation 删除所有
delete from Friends where ids = 5
3.修改数据
update Friends set fcode='p016' 修改所有
update Friends set fcode='p006',mcode='p002' where ids=4
查询:
1.简单查询
select * from Info --查所有数据
select Code,Name from Info --查指定列的数据
select Code as '代号',Name as '姓名' from Info --给列指定别名
2.条件查询
select * from Info where Code='p001'
select * from Info where Sex='true' and Nation='n001' --多条件并的关系
select * from Info where Sex='true' or Nation='n001' --多条件或的关系
3.范围查询
select * from Car where Price>40 and Price<50
select * from Car where Price between 40 and 50
4.离散查询
select * from Car where Code in ('c001','c005','c010','c015')
select * from Car where Code not in ('c001','c005','c010','c015')
5.模糊查询
select * from Car where Name like '%宝马%' --查包含宝马的
select * from Car where Name like '宝马%' --查以宝马开头的
select * from Car where Name like '%宝马' --查以宝马结尾的
select * from Car where Name like '宝马' --查等于宝马的
select * from Car where Name like '__E%' --查第三个字符是E的
% 代表是任意多个字符
_ 代表是一个字符
6.排序查询
select * from Car order by Price asc --以价格升序排列
select * from Car order by Price desc --以价格降序排列
select * from Car order by Oil desc,Price asc --以两个字段排序,前面的是主条件后面的是次要条件
7.分页查询
select top 5 * from Car
select top 5 * from Car where Code not in (select top 5 Code from Car)
当前页:page = 2; 每页显示:row = 10;
select top row * from Car where Code not in (select top (page-1)*row Code from Car)
8.去重查询
select distinct Brand from Car
9.分组查询
select Brand from Car group by Brand having count(*)>2
10.聚合函数(统计查询)
select count(*) from Car --查询所有数据条数
select count(Code) from Car --查询所有数据条数
select sum(Price) from Car --求和
select avg(Price) from Car --求平均
select max(Price) from Car --求最大值
select min(Price) from Car --求最小值
use zuoye
create table
a
高级查询
1.连接查询
select * from Info,Nation --形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code
select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code
select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式
2.联合查询
select Code,Name from Info
union
select Code,Name from Nation
3.子查询
一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。
--查询民族为汉族的所有人员信息
select * from Info where Nation = (select Code from Nation where Name = '汉族')
(1)无关子查询
子查询可以单独执行,子查询和父查询没有一定的关系
--查询系列是宝马5系的所有汽车信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = '宝马5系')
(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)
SQL数据库语言基础的更多相关文章
- Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)
day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库: 简称:DataBase ---->DB 数据库即存放数据的仓库, ...
- SQL数据库的基础操作
一,认识SQL数据库 美国Microsoft公司推出的一种关系型数据库系统.SQLServer是一个可扩展的.高性能的.为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有 ...
- SQL数据库入门基础
SQL(Structure Query Language,结构化查询语言)语言是国际标准化组织(ISO)采纳的标准数据库语言. 数据库就是一幢大楼,我们要先盖楼,然后再招住户(住户当然就是数据库对 ...
- SQL 数据库语言分析总结(二)
介绍sql语言 我们接着一的顺序继续介绍这个语言 数据类型 整形: TINYINT(8位) SMALLINT(16位) MEDIUMINT(24位) INT(32位) BIGINT(64位) 实数: ...
- SQL 数据库语言分析总结(一)
SQL语言是被广泛采用的数据库的学习语言,之前在本科的时候已经学习过了,但是后来又忘记了,所以这次简单的总结一下. 分类 交互式sql语言,交互式语言主要是利用一些数据库工具,比如mysql的终端工具 ...
- SQL server 语言基础
数据库: 1. 结构化查询语言(Structured Query Language)简称SQL: 数据库管理系统(Database Management System)简称DBMS: 数据库管理员(D ...
- sql数据库的基础语句
1, 创建数据库 create database database-name 2, 删除数据库 drop database dbname 3, 备份sql server 创建 备份数据的device ...
- SQL 数据库语言分析总结(三)
这次介绍通过mysql-WorkBench这个工具来管理操作数据库. 创建和删除数据库 1.点击创建数据库按钮 2.选中后右键,出现drop schema一项,这个用来删除. 设置默认数据库 选中右键 ...
- sql数据库语言练习,增删改查
数据库创建 DROP DATABASE IF EXISTS `sql_invoicing`; CREATE DATABASE `sql_invoicing`; USE `sql_invoicing`; ...
随机推荐
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
在操作hibernate数据库时,调用saveOrUpdate方法进行更新保存对象时, (1)ID为null时执行SAVE,但是前端jsp通过<input type="hidden&q ...
- 基于cocos2d-x-3.2学习Box2D(一)
cocos版本号:cocos2d-x-3.2 环境:Win7+VS2013 因为一些太底层的实现我如今的能力学习不到,仅仅能做一些简单的笔记,供以后翻阅.假设别人可以得到帮助,莫大的荣幸. 一.创建世 ...
- Java 实现一个链表
public class MyList { static class Node {// 节点类 Object data; Node next; public Node(Object data) {// ...
- OpenLayers 3+Geoserver+PostGIS实现点击查询
WebGIS开发中,点击查询是最经常使用的一种查询方式,在ArcGIS api 中.这样的查询叫IdentifyTask,主要作用是前台提交參数.交ArcServer查询分析返回. 本文从开源框架的角 ...
- UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)
解题报告 题意: 求全部路中最大分贝最小的路. 思路: 类似floyd算法的思想.u->v能够有另外一点k.通过u->k->v来走,拿u->k和k->v的最大值和u-&g ...
- VirtualMachineManager
Java Code Examples for com.sun.jdi.VirtualMachineManager https://www.programcreek.com/java-api-examp ...
- npm won't install packages “npm ERR! network tunneling socket could not be established, cause=Parse Error”
昨天在使用npm安装react-native的时候一直报网络不能connection,可是在浏览器中直接访问时是成功,搜索百度无果,最后在google中找到了这个解决方案:http://stackov ...
- Python获得文件时间戳 异常访问监控 邮件定时提醒
Python获得文件时间戳 异常访问监控 邮件定时提醒
- leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...