Basic use of SQL Server and simple CRUD operations

A few words at the beginning
Unexpectedly, in the rest of my life, there will be a time to write a blog (no complaint, just a little surprised), the database wyg teacher (David) asked us to write the operation in each experimental class or some usual summary on the blog, through This way to check our homework and learning situation, I have to say, keeping up with the times, and convenient and efficient, wonderful! In that case, I should roll up my sleeves and move bricks to work hard to consolidate the knowledge I have learned!
One, SQL Server installation and configuration

Attached here are two reference blogs (David said it was written by seniors, so I’ll just use prostitutes for nothing), which details each step of the operation and related configuration. There is also an article I read when I installed it myself. In other words, I have always installed things smoothly, so there are no problems. . . If you want to install the package or the installation manual, you can comment or leave a private message, and reply to the
SQL server2019 installation tutorial in time after you see it.
SQL Server create a login name and user name [Detailed introduction]
SQL Server2019 installation steps

2. Basic usage (based on GUI)
1. Create a new database, and pay attention to the system database not to be modified at will to prevent important information from being changed

Insert picture description here

2. Modify and delete the database

Insert picture description here

3. Build table + design table + insert data

Insert picture description here
Insert picture description here

Here you can set the attributes (fields) of the table, such as whether NULL values ​​are allowed (that is, whether the item is required, if not, the default value is NULL), whether the primary key, etc., there are SQL statements behind

Insert picture description here
Insert picture description here

3. Basic usage (SQL statement) (First find the new query in the menu bar, and write the SQL statement here)

PS: The software will not automatically capitalize keywords, sqlyog can
SQL Server notes
Single-line comments: -- 注释内容
Multi-line comments: /* 注释内容 */
MySQL commonly used single-line comments are# 注释内容

1. Create a new database and set related properties

Common way of writing (use the default configuration)

create database test;

Complete writing

CREATE DATABASE testdb -- (数据库名称) 
on primary( --(主文件,固定格式:NAME主文件的名称,主文件的路径)
name = 'testdb',
filename = 'D:\temp\testdb.mdf', -- 主文件以mdf结束
size = 10MB, -- 主文件的初始值大小
maxsize = 100,
filegrowth = 5
) -- 数据文件每次的增长量,由于增删改等操作,文件增大,每次增大5MB
log on(
name = 'testdb_log',
filename = 'D:\temp\testdb_log.ldf',
size = 10,
maxsize = 100,
filegrowth = 5
)
2. Table creation + add, delete, modify and query based on single table (CRUD operation)

PS: When deciding to operate a library, it is recommended to select the database in advance to prevent unnecessary errors and troubles, or use SQL statements, such as use student, to switch to the student library
Insert picture description here

PS: Basically all common keywords for operations

Create: create
add, insert: insert
delete: delete, drop
modify: alter, update
query: select

2.1 Table creation

Use commas to separate multiple statements, do not add the last one

create table stuInfo(
id varchar(15) primary key not null, -- 主键(primary key)且不允许为空,主键本身就非空
name varchar(15) not null, -- 不允许空
age int null, -- 允许为空
sex varchar(5),
major varchar(20)
)
2.2 Table modification (mainly refers to attribute fields)
-- 更改字段类型长度
alter tablealter column 字段名 类型长度

alter table stuInfo
alter column name varchar(20)

-- 更改字段类型
alter tablealter column 字段名 更改后的类型

-- 添加非空约束
alter tablealter column 字段名 int not null

-- 设置主键
alter tableadd constraint 主键名 primary key(字段名)

add constraint kid primary key (id)

-- 删除主键
alter tabledrop constraint 主键名

-- 更改字段名
exec sp_rename '表名.字段名',
'更改后的字段名','COLUMN'(表示修改类型是字段),-- 注意COLUMN一定要大写

-- 添加字段名
alter tableadd 字段名 字段类型 default null
2.3 Table deletion
drop table 表名
2.4 Insert data into the table
-- 插入单行数据
insert into '表格名'('栏位1','栏位2',...)
values('值1','值2',...);

insert into stuInfo(id,name,age,sex,major) 
values('123','haha','10','男','金融') -- 按顺序写

-- 插入多行数据
insert into '表格名'('栏位1','栏位2',...) values
('值1','值2',...),
('值1','值2',...),
('值1','值2',...);
-- 从其他表copy数据
insert into '表格1'('栏位1','栏位2',...)
select '栏位3','栏位4',... -- 注意要和上边的栏位数目一致
from '表格2';

Insert picture description here

2.5 Modify the data in the table
update 表名 set 字段 =update stuInfo set age = 8
where id = '123' -- where指定查询,会发现id=123的age年龄变成了8

Insert picture description here
Insert picture description here

2.6 Delete data in the table
-- 图形界面删除的话打开表,选中要删除行,右键删除即可

delete from stuInfo -- 会删除所有内容
-- 熟练之后可以省掉from
delete from 表名
where id = '123' -- 只删除id=123的那一行数据
2.7 Query the data in the table
-- select 字段名(若为*代表查询所有字段信息) from 表名

-- 查询所有数据
select * from userInfo;

-- distinct 去重操作,只显示不一样的
select distinct 字段名 from 表名

-- top 查询前多少行数据
select top 行数 字段名 from 表名

-- 指定输出格式
select grade+'-'+className from stuInfo -- 显示为一年级-1班,对结果进行拼接

Insert picture description here

4. Remarks:

The above SQL statement is similar to what I learned before, but I haven't fully memorized it yet. . Need to remember + practice. OK, the task is complete, start cooking!

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/114524964
Recommended