欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL-SERVER学习(一) 数据表的基本操作

程序员文章站 2022-06-21 20:58:36
1、创建一个数据库,数据库的名字是UserInfo create database UserInfo; 2、创建一个数据表,数据表的名字是UserInfocreate table UserInfo( UserId int primary key identity(1, 1), UserName va ......

1、创建一个数据库,数据库的名字是UserInfo

  create database UserInfo;

 

2、创建一个数据表,数据表的名字是UserInfo
create table UserInfo
(

UserId int primary key identity(1, 1),
UserName varchar(50),
Pwd varchar(20),
Age int,
Gender char(5)

)


3、向表格插入数据
insert into UserInfo(UserName, Pwd, Age, Gender)
values('张三', 'zhangsan123', 20, '男')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('李四', 'lisi123', 42, '女')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('王五', 'wangwu123', 50, '女')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('刘德华', 'lisi123', 42, '男')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('张学友', 'wangwu123', 50, '男')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('刘亦菲', 'lisi123', 25, '女')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('杨幂', 'wangwu123', 33, '女')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('刘诗诗', 'wangwu123', 18, '女')

insert into UserInfo(UserName, Pwd, Age, Gender)
values('邓紫棋', 'wangwu123', 16, '女')

4、查询
select (
select * ,age as 'number' from UserInfo ) t
where t.Age>50 and t.number=1


select * from UserInfo where Gender = '女' and Age < 20 and UserName like '邓%'

select * from UserInfo

5、更改属性值
update UserInfo set Age = 19 where UserName = '李四'


update UserInfo set Age = 20 where UserName = '李四' or UserName = '王五'

select * from UserInfo where (UserName like '刘%') and (Gender = '女') and (UserName not like '%诗')

select * from UserInfo

6、删除数据
delete from UserInfo where UserId = 10 or UserId = 11