表操作常用SQL语句

2025-10-27 13:49:53

1、--创建表

--创建学生信息表

CREATE TABLE [dbo].[students](

[student_id] [nvarchar](40) NOT NULL,--ID字段

[student_name] [nvarchar](100) NULL,--学生名称字段

[student_no] [nvarchar](100) NULL,--学生号字段

[sex] [nvarchar](10) NULL,--学生性别字段

--学生

 CONSTRAINT [PK_students] PRIMARY KEY CLUSTERED 

(

[student_id] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

2、---删除表

---删除学生表

DROP TABLE [dbo].[students]

GO

3、---判断表是否存在

IF OBJECT_ID ('dbo.students') IS NOT NULL

    --如果存在students表,则删除该表。

DROP TABLE [dbo].[students]

GO

4、---插入数据

INSERT INTO [dbo].[students]

           ([student_id]

           ,[student_name]

           ,[student_no]

           ,[sex])

     VALUES

           (NEWID()

  ,'张三'

  ,'000001'

  ,'男')

GO

5、--查询数据

--查询学生号为000001的学生信息。

SELECT * FROM [students] WHERE student_no='000001'

6、--更新数据

--更新学生号为000001学生性能为女。

UPDATE a SET  sex='女' FROM [students] a WHERE student_no='000001'

7、--删除数据

--删除学生号为000001学生的信息

DELETE FROM [students] WHERE student_no='000001'

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢