The operations in SQLServer2005(SQL)
虽然现在Microsoft的产品SQLServer好像不太受欢迎(从我身边了解到的),这里就简单说说一些SQLServer的SQL,其中很多是标准的SQL语句,数据库通用的。 连接步骤 //前期准备 String driver =com.microsoft.sqlserver.jdbc.SQLServerDriver;URL: String url =jd
虽然现在Microsoft的产品SQLServer好像不太受欢迎(从我身边了解到的),这里就简单说说一些SQLServer的SQL,其中很多是标准的SQL语句,数据库通用的。
连接步骤
//前期准备
String driver =com.microsoft.sqlserver.jdbc.SQLServerDriver; URL: String url =jdbc:sqlserver://localhost:1433;databadeName=db_Blog; USERNAME: String username = sa; PASSWORD: String password = ysjian //按照自己的设定
//利用发射机制创建类的加载和连接
Class.forName(driver); Connection conn =DriverManager.getConnection(url,username,passWord);
//执行预编译
String sql ; String[] param; PreparedStatementpstm = conn.prepareStatement(sql); If(param!=null&?m.length>0){ for(inti=0;i<param.length i pstm.setstring><br> <br> <p>执行查询:ResultSetrs = pstm.executeQuery();</p> <p>执行更新:int result = pstm.executeUpdate();</p> <p> </p> <p><strong>主键(primarykey)</strong>:数据的唯一标识,不会重复的列做主键</p> <p>1. 业务主键:使用有业务意义的字段做主键,如用户名,身份证号,银行账号等(不推荐)</p> <p>2. <span>逻辑主键:</span>使用无任何意义的字段做主键,因为很难保证业务主键不会重复,所以<span>推荐使用逻辑主键</span></p> <p><strong>外键(foreignkey):</strong>在表与表之间建立联系的枢纽,标间关联</p> <p> </p> <p><strong>列的数据类型:</strong></p> <p>bit(0或1):相当于boolean类型的数据; </p> <p>char(n):不可变的字符串,不足部分用空格填充</p> <p>varchar(n):最大长度为8000</p> <p>nvarchar(MAX):类似无限大,2^31-1</p> <p>datetime(时间类型):date</p> <p>timestamp:时间戳,时间格式较全的格式</p> <p>uniqueidentifier:唯一标示符(推荐做主键)</p> <p><strong>主键的选择:</strong></p> <p>1. int(bigint)+标识列(自增字段)</p> <p>2. uniqueidentifier(GUID):<strong>业界主流</strong></p> <p><strong>int自增做主键的优缺点:</strong></p> <p> 优点:占用空间小,无需开发人员干预</p> <p> 缺点:效率低,数据导入导出时不便</p> <p><strong>GUID做主键的优缺点<br> </strong> 优点:效率高,数据的导入导出方便</p> <p> 缺点: 占用空间大,不易读</p> <p><strong>SQL语句</strong></p> <p><strong> </strong></p> <p><strong><u>◎插入语句</u></strong></p> <p><strong>int自增做主键:</strong></p> <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">insert into users values(‘ysjian’,22)--自增主键可以不给值 insert into users(name,age) values(‘ysjian’,22)--推荐带上列名
uniqueidentifier做主键:
insert into users values(‘ysjian’,22)--自增主键可以不给值 insert into users(name,age) values(‘ysjian’,22)--推荐带上列名
--表示不等于20
update users set name=N’袁’ where age20 update users set name=N’袁’ where age!=20 update users set name=N’袁’ where age>20 and age <p><strong><u>◎删除语句</u></strong></p> <p></p> <pre class="brush:php;toolbar:false"> delete from users--清空表(注意delete后面不能加*) delete from users where age=20
◎查询语句(重点)
select* from users select name as ‘姓名’,ageas 年龄,id as ‘编号’from users select ‘姓名’ =name , 年龄= age,id as ‘编号’from users select age+3 as 年龄 from users
//聚合函数
Select count(*) from users Select max(age) from users Select min(age) from users Select avg(age) from users Select from users
//排序
Select * from users order by age desc--按年龄降序 Select * from users where age>20 order by age asc--按年龄升序
//模糊查询(通配符’_’和’%’)
Select * from users where name like‘袁_’--查询以”袁”开头后面有一个字符 Select * from users where name like‘%袁%’--查询名字有”袁”字的数据
//null(不知道)
Select * from users where name is null Select null+1--结果为null Select null+’123’--结果为null
//分组查询
Select age ,count(*) from users group by age --查询的列名必须与分组一致,聚合函数不能出现在where子句中 (错)Select count(*) from users where count(*)>5 group by age(错)--having子句是对分组后的信息过滤,能用的列是查询的列 (错)Select count(*) from users group by age having id>5(错) Selectage, count(*) from users group by age having age>20 and count(*)>5
//限行查询
Select top 5 * from users order by age desc Select top 5 percent * from users
//经典例子:按工资从高到低的排序检索从第六名开始一共五人信息
Select top 5 salary from employee where id not in(select top 5 id from users order by salary desc) order by salary desc
//保持数据的唯一
Select distinct eName fromemployee--保持整行数据的唯一性
//联合查询,上下字段的个数必须一致,且数据类型相容
Select name,age from users Union all--默认会将完全重复的数据合并,all可以阻止合并 Select name,5 from users2
//(联合查询的运用)报表的制作
Select ‘正式工最大年龄’,max(fAge) from T_employee Union all Select ‘正式工最小年龄’,min(fAge) from T_employee Union all Select ‘临时工最小年龄’,min(fAge) from T_tempEmployee Union all Select ‘临时工最大年龄’,max(fAge) from T_tempEmployee Select FNumber,FSalary from T_Employee Union all Select ‘工资合计’,sum(Fsalary) from T_Employee
//数据库函数
Select ABS(-5)--绝对值5 Select ceiling(5.2) --大于5.2的最小整数 Select floor(-3.5)--小于-3.5的最大整数 Select round(3.1415926,3)--四舍五入,指定取舍位3,结果为3.1420000 Select len(‘abc’)--3 Select lower(‘ABC’)--abc Select upper(‘abc’)--ABC Select ltrim(‘ china ’)--china Select rtrim(‘ china ’)-- china Select substring(‘yuanshenjian’,3,5)--开始位置为3,长度为5
//日期函数
Select getdate();--取得当前日期 Select daeAdd(day,5,getdate())--当前时间天数加3 Select dateDiff(day,’1990-08-02’,getdate())--1990-08-02距离当前时间的天数 Select datePart(year,getDate())--返回一个日期的特定部分
//经典语句
Select dateDiff(year,FinDate,getDate()), count(*) from T_Employee Group by dateDiff(year,FinDate,getDate()) Having count(*)>2
//类型转换
Select cast (‘123’asint),cast(‘2012-11-23’as datetime) Select convert(datetime,’2012-11-23’),convert(varchar(50),123)
//流控函数,如果FName为null,赋值为“佚名”
Select isnull(FName,’佚名’) as 姓名 from T_Employee
//单值判断
Select FName, ( case Flevel when 1 then‘普通客户’ when 2 then‘会员’ when 3 then‘VIP’ else ‘未知客户类型’ end--一定要加end )as 客户类型 from T_Customer
//l练习:表中有A,B,C三列,但A大于B时选A,否则选B,但B大于C时
选B,否则选C
Select ( case when A>B then A else B end ), ( case when B>C then B else C end ) From player
//练习二
Select Name as 队名, sum( case scores when ‘胜’ then 1 else 0 end )as 胜, sum( case scores when ‘负’ then 1 else 0 end )as 负 from Team group by Name
//数据库的创建
if exsits(select * from sys.database when [name]=’market’) drop database market create database market on ( name=’market.mdf’, filename=’E:\Microsoft\market.mdf’, size=5, maxsize=555, filegrowth=55 ) log on ( name=’market’, filename=’market.ldf’, size=5, maxsize=55, filegrowth=55% )
//表的创建
if exists(select * from sys.objects where[name]=’employee’) drop table employee create table employee ( eId varchar(5) not null primary key, eSex bit not null default(1), uidint not null identity pid varchar not null foreign key references employees(pId) )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Editor of Machine Power Report: Wu Xin The domestic version of the humanoid robot + large model team completed the operation task of complex flexible materials such as folding clothes for the first time. With the unveiling of Figure01, which integrates OpenAI's multi-modal large model, the related progress of domestic peers has been attracting attention. Just yesterday, UBTECH, China's "number one humanoid robot stock", released the first demo of the humanoid robot WalkerS that is deeply integrated with Baidu Wenxin's large model, showing some interesting new features. Now, WalkerS, blessed by Baidu Wenxin’s large model capabilities, looks like this. Like Figure01, WalkerS does not move around, but stands behind a desk to complete a series of tasks. It can follow human commands and fold clothes

Skyworth Security recently released its annual flagship new product - Skyworth Smart Screen Camera S50. As the first smart screen camera equipped with artificial intelligence technology in the industry, the biggest feature of Skyworth Smart Screen Camera S50 is that it cleverly combines a color smart screen with a camera to realize the innovative function of two-way visual calls; its powerful 2T computing power makes it The artificial intelligence capability has been greatly improved. This new flagship product of the year has a futuristic appearance, fine frosted material, and is equipped with a colorful touchable high-definition screen with smooth and smooth operation; two-way video intercom, WeChat video call; 500W Extremely clear picture quality, 360° monitoring without blind spots; extreme black light full-color night vision, no matter how dark it is, it is as colorful as daytime; 12x smart zoom, you can see every detail clearly when you zoom in; the rewritten content is: 2

What currency is THE? THE (Tokenized Healthcare Ecosystem) is a digital currency that uses blockchain technology to focus on innovation and reform in the healthcare industry. THE coin's mission is to use blockchain technology to improve the efficiency and transparency of the medical industry and promote more efficient cooperation among all parties, including patients, medical staff, pharmaceutical companies and medical institutions. The Value and Characteristics of THE Coin First of all, THE Coin, as a digital currency, has the advantages of blockchain - decentralization, high security, transparent transactions, etc., allowing participants to trust and rely on this system. Secondly, the uniqueness of THE coin is that it focuses on the medical and health industry, using blockchain technology to transform the traditional medical system and improve

How to check the latest price of TheSandbox currency TheSandbox is a decentralized gaming platform built on the Ethereum blockchain. Land, assets and gaming experiences can be purchased using its native token SAND. The steps to check the latest price of SAND are as follows: Choose a reliable price check website or app. Some commonly used price query websites include: CoinMarketCap: https://coinmarketcap.com/Coindesk: https://www.coindesk.com/Binance: https://www.binance.com/ Search on the website or app SAND. View SAND

How to check the latest price of TheGraph coin? TheGraph is a decentralized protocol designed to provide efficient indexing and query services for blockchain data. The protocol is designed to make it easier for developers to build and launch decentralized applications (dApps), and to provide these applications with convenient access to blockchain data. To check the latest price of TheGraph Coin (GRT), you can follow these steps: Choose a reliable price checking website or app. Some commonly used price query websites include: CoinMarketCap: https://coinmarketcap.com/Coindesk: https://www.coind

Samsung plans to launch a new generation of Galaxy Z Fold and Flip 6 series folding screen smartphones in the second half of this year. Recently, Korean media TheElec and "Jiji Weekly e" revealed more details about these two new products. Samsung Galazy Z Fold6 leaked pictures. Source @chunvn8888 According to TheElec, Samsung Electronics’ supply chain manufacturers are expected to start the production of Galaxy Z Fold6 and Flip 6 related components in early May. In contrast, the production of parts for Galaxy Z Fold5 and Flip 5 started in the second half of May last year. This means that this year’s release schedule for the standard version of the Galaxy Z series is about two to three weeks earlier than last year. go

Recently, I read the enterprise desktop configuration white paper produced by Logitech in the first half of the year. The knowledge and purchasing logic involved in enterprise-level desktop peripherals gave us a lot of inspiration. Many of these fresh viewpoints are very suitable to be shared with old fans of Zhongguancun. Logitech White Paper: New Thoughts on Purchasing Desktop Peripherals As a leader in the field of desktop peripherals, Logitech’s brand strength and technological innovation are obvious to all. The significance of the release time of the white paper The release time of Logitech’s white paper coincides with the transformation of corporate office models. The popularity of hybrid office models poses new challenges for employer branding and talent attraction. New Trends in Desktop Peripheral Purchasing Previous desktop peripheral purchasing standards may have been too simplistic. Employees in different positions have significantly different needs for keyboards, mice, headsets and cameras. Perspectives in Logitech White Paper Logitech White

How to Check TheGraph Coin Market Cap TheGraph is a decentralized protocol designed to help developers index and query blockchain data. Its token GRT is used to pay network fees and reward node operators. How to check the market value of TheGraph currency: Choose a reliable website or platform: There are multiple websites and platforms that provide cryptocurrency market value information, such as CoinMarketCap, CoinGecko, Feixiaohao, etc. It is important to choose a reliable website or platform to ensure you are getting accurate information. Search for TheGraph: Search for GRT or TheGraph on the website or platform. View Market Cap: TheGraph’s market cap is often shown in search results. Tip: market capitalization
