头歌实践平台--数据库原理

简介: 头歌实践平台--数据库原理

默认

> sqlcmd -S localhost -U sa -P '<123123Aa!@>'
> create database MyDb
> use MyDb

外键约束

> create table t_class(id int,name varchar(22),primary key(id))
> create table t_student(id int primary key,name varchar(22),classId int,constraint fk_stu_class1 foreign key(classId) references t_class(id))

添加常用约束

> create table t_user(id int primary key identity(1,1) not null,username varchar(32)  not null unique,sex int default 0)

数据的插入

-- ********** create database ********** --
-- ********** Begin ********** --
create database school



-- ********** End ********** --
go

use school
go

-- ********** create table ********** --
-- ********** Begin ********** --

create table teacher(ID int not null,Name varchar(20) not null,sex char(2) not null,Phone varchar(20) null)



-- ********** End ********** --
go

SET NOCOUNT ON


-- ********** insert ********** --
-- ********** Begin ********** --
insert into teacher values (1,"Lucy",'F',null)


-- ********** End ********** --
go

数据的删除

-- ********** create database ********** --
-- ********** Begin ********** --

create database website
-- ********** End ********** --
go

use website
go

-- ********** create table ********** --
-- ********** Begin ********** --

 create table shopping(
     ID int identity(1,1) not null,
     Name varchar(20) not null,
     address varchar(30) not null
 )   
    
-- ********** End ********** --
go

SET NOCOUNT ON

insert into shopping (Name, address) values ('eBay', 'www.ebay.com')
go

SET NOCOUNT ON

-- ********** insert ********** --
-- ********** Begin ********** --

insert into shopping (Name, address) values ('amazon', 'www.amazon.com')
-- ********** End ********** --
go

SET NOCOUNT ON


-- ********** delete ********** --
-- ********** Begin ********** --
delete from shopping where ID = 1

-- ********** End ********** --
go

数据的更改

-- ********** create database ********** --
-- ********** Begin ********** --
create database Books
-- ********** End ********** --
go

use Books
go

-- ********** create table ********** --
-- ********** Begin ********** --
create table prices(
    ID int identity(1,1) not null,
    Name varchar(20) not null,
    price varchar(30) not null
)

-- ********** End ********** --
go

SET NOCOUNT ON

-- ********** insert ********** --
-- ********** Begin ********** --
insert into prices (Name,price) values ('Harry Potter','$128')

-- ********** End ********** --
go

SET NOCOUNT ON

insert into prices (Name, price) values ('Walden', '$5')
go

SET NOCOUNT ON

-- ********** update ********** --
-- ********** Begin ********** --

update prices
set price = '$6'
where Name ='Walden'

-- ********** End ********** --
go

AVG() 函数的使用

USE Mall
GO

SET NOCOUNT ON

------ return two columns that the price bigger than average price ------
-- ********** Begin ********** --
select prod_name,prod_price
from Products
where prod_price> (
    select avg(prod_price) from Products
)

-- ********** End ********** --

GO

COUNT() 函数的使用

USE Mall
GO

SET NOCOUNT ON

------ return the number of product which price bigger than 10 -----
-- ********** Begin ********** --

select count(prod_price) from Products where prod_price > 10

-- ********** End ********** --

GO

MAX() 函数和 MIN() 函数的使用

USE Mall
GO

SET NOCOUNT ON

------ return the price of the least expensive item ------
-- ********** Begin ********** --

select prod_name,prod_price from Products where prod_price = (
    select min(prod_price) from Products
)

-- ********** End ********** --

GO

SUM() 函数的使用

USE Mall
GO

SET NOCOUNT ON

------ return the amount of all products ------
-- ********** Begin ********** --
select sum(prod_price * quantity) as amount
from Products


-- ********** End ********** --

GO

带 WHERE 子句的多表查询

USE Mall
GO

SET NOCOUNT ON

--********** Begin **********--
select * from Products p,Vendors v where p.vend_id = v.vend_id

--********** End **********--

GO

内连接查询

USE Mall
GO

SET NOCOUNT ON

--********** Begin **********--

select p.*,v.vend_name,v.vend_phone
from Products p inner join Vendors v
on p.vend_id = v.vend_id

--********** End **********--

GO
相关文章
|
21小时前
|
小程序 JavaScript 关系型数据库
乡村研学|乡村研学小程序|基于微信小程序的乡村研学平台设计与实现(源码+数据库+文档)
乡村研学|乡村研学小程序|基于微信小程序的乡村研学平台设计与实现(源码+数据库+文档)
4 0
|
21小时前
|
小程序 JavaScript Java
驾校预约|驾校预约小程序|基于微信小程序的驾校预约平台设计与实现(源码+数据库+文档)
驾校预约|驾校预约小程序|基于微信小程序的驾校预约平台设计与实现(源码+数据库+文档)
5 0
|
21小时前
|
安全 JavaScript Java
租房招聘|在线租房和招聘平台|基于Springboot的在线租房和招聘平台设计与实现(源码+数据库+文档)
租房招聘|在线租房和招聘平台|基于Springboot的在线租房和招聘平台设计与实现(源码+数据库+文档)
2 0
|
22小时前
|
JavaScript Java 关系型数据库
流浪动物救助|基于Springboot的流浪动物救助平台设计与实现(源码+数据库+文档)
流浪动物救助|基于Springboot的流浪动物救助平台设计与实现(源码+数据库+文档)
5 0
|
7天前
|
存储 Java 分布式数据库
【分布式计算框架】HBase数据库编程实践
【分布式计算框架】HBase数据库编程实践
15 1
|
7天前
|
SQL Java 数据库连接
Java数据库编程实践:连接与操作数据库
Java数据库编程实践:连接与操作数据库
12 0
|
7天前
|
存储 监控 Apache
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
网易的灵犀办公和云信利用 Apache Doris 改进了大规模日志和时序数据处理,取代了 Elasticsearch 和 InfluxDB。Doris 实现了更低的服务器资源消耗和更高的查询性能,相比 Elasticsearch,查询速度提升至少 11 倍,存储资源节省达 70%。Doris 的列式存储、高压缩比和倒排索引等功能,优化了日志和时序数据的存储与分析,降低了存储成本并提高了查询效率。在灵犀办公和云信的实际应用中,Doris 显示出显著的性能优势,成功应对了数据增长带来的挑战。
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
|
7天前
|
存储 算法 数据库
矢量数据库在图像识别与检索中的应用实践
【4月更文挑战第30天】本文探讨了矢量数据库在图像识别与检索中的应用,通过特征提取(如SIFT、SURF)、编码和相似度度量实现快速识别。在图像检索流程中,经过预处理、特征提取和编码后,矢量数据库用于查询相似特征,排序后展示给用户。实际案例显示,矢量数据库能提升电商平台的商品图像搜索效率和用户体验。随着技术发展,这一领域应用前景广阔。
|
7天前
|
数据管理 关系型数据库 MySQL
数据管理DMS产品使用合集之DMS可以接入其他平台的MySQL数据库,是否还支持无感知变更功能
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
7天前
|
存储 SQL 数据库
数据库设计案例:电商系统数据库设计实践
数据库设计案例:电商系统数据库设计实践
70 1