当前位置:网站首页>sql server建表时设置ID字段自增 (navicat 演示)
sql server建表时设置ID字段自增 (navicat 演示)
2022-07-17 08:57:00 【逍遥游@】
引言:我们创建sql sever如何设置id自增呢,它跟mysql又有 哪些区别呢
今天我们拿一个简单的表user做例子
Class是数据库,sqlser里有个dbo,意思是对所有的数据库使用者有效
我们查询的时候比如select * from [dbo].[user];这样才能查询到,跟myslq的不一样

打开选项,选中标识字段id(只有整形的数据变量才可以设置自增),标识种子1,就是从1开始,增量为1,就会每条数据从1、2、3.。。。,如果增量为 2,就是1、3、5.。。。。

/*创建表的代码。
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : SQL Server
Source Server Version : 11002100
Source Host : localhost:1433
Source Catalog : Class
Source Schema : dbo
Target Server Type : SQL Server
Target Server Version : 11002100
File Encoding : 65001
Date: 23/11/2019 09:57:16
*/
-- ----------------------------
-- Table structure for user
-- ----------------------------
IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[user]') AND type IN ('U'))
DROP TABLE [dbo].[user]
GO
CREATE TABLE [dbo].[user] (
[id] int IDENTITY(1,1) NOT NULL,
[username] varchar(255) COLLATE Chinese_PRC_CI_AS NULL,
[password] varchar(255) COLLATE Chinese_PRC_CI_AS NULL
)
GO
ALTER TABLE [dbo].[user] SET (LOCK_ESCALATION = TABLE)
GO
-- ----------------------------
-- Primary Key structure for table user
-- ----------------------------
ALTER TABLE [dbo].[user] ADD CONSTRAINT [PK__user__3213E83FD0A7E4CC] PRIMARY KEY CLUSTERED ([id])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF)
ON [PRIMARY]
GO
jdbc连接sqlsever需要更改配置,和一个sqlserver的驱动jar包
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
final static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
final static String URL="jdbc:sqlserver://localhost:1433;DatabaseName=CLASS";//2000版和2008版本可能不太一样。
final static String USER="sa";
final static String PASSWORD="123456";
public static Connection getConnection(){
try{
Class.forName(DRIVER);
Connection connection=DriverManager.getConnection(URL,USER,PASSWORD);
return connection;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void closeConnection(Connection c){
try{
c.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
System.out.println("连接成功"+DBConnection.getConnection());
}
}
希望帮到大家
边栏推荐
猜你喜欢
随机推荐
Collation of exercises of shallow neural networks in the third week of in-depth study
软件工程师视角的项目合同分析
Zero basic C language
小说里的编程 【连载之十三】元宇宙里月亮弯弯
Leetcode sword finger offer II 041 Average value of sliding window: low space consumption solution
Xgen hair guide history cleared solution
Magic Usage of mongodb $symbol +mongo data type
Introduction to deep learning exercises sorting in the first week of deep learning
[leetcode] general operation summary
最新水果FLStudio20.9低版本版升级高版本教程
Express
零基础C语言
MySQL视图
【CTF】pwn2_ sctf_ two thousand and sixteen
Sorting out of neural network basics exercises in the second week of in-depth study
scratch逆序输出 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
[face recognition] face recognition based on histogram histogram with matlab code
Megatexture technology of ID tech5
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
LeetCode 0115. Different subsequences









