SqlServer数据库的语句及一些操作整理
临近年终,在工作之余对工作和学习中遇到的问题以及常用的一些知识点做了些整理,以备后用。本文涉及的内容为数据库,算是对开发总结(1)---数据库一文的补充。 1 对于主键设置了Identity的表,在删除表中数据后再往表中插入数据,Identity列不是从1起始了,如
临近年终,在工作之余对工作和学习中遇到的问题以及常用的一些知识点做了些整理,以备后用。本文涉及的内容为数据库,算是对开发总结(1)---数据库一文的补充。
1 对于主键设置了Identity的表,在删除表中数据后再往表中插入数据,Identity列不是从1起始了,如果想删除数据后Indentity列仍从1起始,可以用下面代码来删除数据。
<span>truncate table </span><span>tablename </span><span>DBCC </span><span>CHECKIDENT</span><span>(</span><span>tablename</span><span>,</span><span>RESEED</span><span>,</span><span>1</span><span>)</span>
2 判断指定表在数据库中是否存在
<span>if </span><span>exists(</span><span>select name from </span><span>sysobjects </span><span>where name</span><span>=</span><span>'tablename' </span><span>and </span><span>type</span><span>=</span><span>'u'</span><span>)</span>
3 判断指定列在指定表中是否存在
<span>if </span><span>exists(</span><span>select </span><span>* </span><span>from </span><span>sys.columns</span><span>,</span><span>sys.tables </span><span>where </span><span>sys.columns</span><span>.</span><span>object_id </span><span>= </span><span>sys.tables</span><span>.</span><span>object_id </span><span>and </span><span>sys.tables</span><span>.</span><span>name</span><span>=</span><span>'tablename' </span><span>and </span><span>sys.columns</span><span>.</span><span>[name]</span><span>=</span><span>'columnname'</span><span>)</span>
4 在编写代码生成器之类的程序的时候,通常需要取出数据库中所有的表名以及表中字段的一些基本信息,如字段长度、字段类型、描述等。实现上面要求的sql语句如下:
<span>--取数据库中表的集合 </span><span>select </span><span>* </span><span>from </span><span>sysobjects </span><span>where </span><span>xtype</span><span>=</span><span>'u' </span><span>order by name </span><span>--取表中字段的一些基本信息 </span><span>select </span><span>sys.columns</span><span>.</span><span>name</span><span>, </span><span>--字段名 sys.types</span><span>.</span><span>name as </span><span>typename</span><span>, </span><span>--字段类型 sys.columns</span><span>.</span><span>max_length</span><span>, </span><span>--字段长度 sys.columns</span><span>.</span><span>is_nullable</span><span>, </span><span>--是否可空 </span><span>(</span><span>select </span><span>count</span><span>(*) </span><span>from </span><span>sys.identity_columns </span><span>where </span><span>sys.identity_columns</span><span>.</span><span>object_id </span><span>= </span><span>sys.columns</span><span>.</span><span>object_id </span><span>and </span><span>sys.columns</span><span>.</span><span>column_id </span><span>= </span><span>sys.identity_columns</span><span>.</span><span>column_id </span><span>) </span><span>as </span><span>is_identity </span><span>,</span><span>--是否自增 </span><span>(</span><span>select value from </span><span>sys.extended_properties </span><span>where </span><span>sys.extended_properties</span><span>.</span><span>major_id </span><span>= </span><span>sys.columns</span><span>.</span><span>object_id </span><span>and </span><span>sys.extended_properties</span><span>.</span><span>minor_id </span><span>= </span><span>sys.columns</span><span>.</span><span>column_id </span><span>) </span><span>as </span><span>description </span><span>--注释 </span><span>from </span><span>sys.columns</span><span>, </span><span>sys.tables</span><span>, </span><span>sys.types </span><span>where </span><span>sys.columns</span><span>.</span><span>object_id </span><span>= </span><span>sys.tables</span><span>.</span><span>object_id </span><span>and </span><span>sys.columns</span><span>.</span><span>system_type_id</span><span>=</span><span>sys.types</span><span>.</span><span>system_type_id </span><span>and </span><span>sys.tables</span><span>.</span><span>name</span><span>=</span><span>'tablename' </span><span>order by </span><span>sys.columns</span><span>.</span><span>column_id</span>
5 在存储过程中使用事务
<span>create procedure </span><span>procname </span><span>as begin tran </span><span>--执行sql语句 </span><span>if </span><span>@@ERROR</span><span>!=</span><span>0 </span><span>begin rollback tran </span><span>--失败 </span><span>end else begin commit tran </span><span>--成功 </span><span>end </span>
6 清除数据库日志
<span>DUMP TRANSACTION </span><span>DatabseName </span><span>WITH </span><span>NO_LOG </span><span>BACKUP </span><span>LOG </span><span>DatabseName </span><span>WITH </span><span>NO_LOG </span><span>DBCC </span><span>SHRINKFILE</span><span>(</span><span>DatabseLogName</span><span>,</span><span>1</span><span>) </span><span>--DatabseName为数据库名称 --DatabseLogName为日志文件名,可以通过下面语句得到 --select name from sysfiles </span>
还有一种比较简单的方法是分离数据库,删除日志文件,再附加数据库,这样产生的日志文件只有500多k。
下面介绍几个常用的系统存储过程和函数
7 db_name() 得到数据库名称
<span>select </span><span>db_name</span><span>() </span><span>Test </span><span>(</span><span>1 行受影响</span><span>)</span>
8 object_id 可以得到对象在系统中的编号,对象包括表、视图、存储过程等。如果不存在返回null,所以也可以用来判断表是否存在。
<span>select </span><span>object_id</span><span>(</span><span>'objectname'</span><span>) </span><span>--判断表是否存在 </span><span>if </span><span>object_id</span><span>(</span><span>'tablename'</span><span>) is not null</span>
9 sp_helptext 用来得到视图、存储过程等对象的文本,可以很快速找到,不过会改变视图或存储过程的格式。所以这个系统存储过程我通常都是用来查看,如果要修改一个存储过程我还是会通过树形菜单去找到存储过程然后修改保存。
<span>sp_helptext </span><span>'objectname'</span>
10 parsename,可以得到对象名称的指定部分,该函数有两个参数,第一个为对象名称,第二个为指定部分的代号。
<span>select </span><span>parsename</span><span>(</span><span>'oec2003.databasename.dbo.tablename'</span><span>,</span><span>1</span><span>) </span><span>--对象名称返回tablename </span><span>select </span><span>parsename</span><span>(</span><span>'oec2003.databasename.dbo.tablename'</span><span>,</span><span>2</span><span>) </span><span>--Schema名称返回dbo </span><span>select </span><span>parsename</span><span>(</span><span>'oec2003.databasename.dbo.tablename'</span><span>,</span><span>3</span><span>) </span><span>--数据库名称返回databasename </span><span>select </span><span>parsename</span><span>(</span><span>'oec2003.databasename.dbo.tablename'</span><span>,</span><span>4</span><span>) </span><span>--服务器名称返回oec200</span>
先就写这么多吧,后面整理出来的会陆续补上。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

The location where the Navicat database configuration files are stored varies by operating system: Windows: The user-specific path is %APPDATA%\PremiumSoft\Navicat\macOS: The user-specific path is ~/Library/Application Support/Navicat\Linux: The user-specific path is ~/ .config/navicat\The configuration file name contains the connection type, such as navicat_mysql.ini. These configuration files store database connection information, query history, and SSH settings.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

The format of the Navicat connection URL is: protocol://username:password@host:port/database name? Parameters, which contain the information required for the connection, including protocol, username, password, hostname, port, database name and optional parameter.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.
