SQL Server database collation notes (a)

  • Database: persistent storage to optimize read and write, to ensure the validity of the data. Relational databases are based on ER model (ie solid - model), use the SQL language to operate.
  • Database Category: document database, service-oriented database (using mostly) (ie, mobile phones are using sqlite end document database)
  • Three paradigms: the column can not be split, the unique identifier, the primary key referenced
  • Relations and storage:
1 to 1 1 to many Many to many
A 1 B 1 of Several B 1 A Several B 1 A
1 1 A to B 1 1 A to B A 1 B Several
Relationship memory A or B Relations exist B New relationships exist Table C
  • Database file: 1 .mdf master data file (startup database containing information, and store the data) 2. The auxiliary data file i.e. log file .ldf
  • A database and only one master file
  • At least one log file
  • Database is divided into: a user database (user-defined) system databases (5, i.e. master, Model, tempdb, msdb, resource (not shown in the box software))
  • Database objects: tables, data types, views, indexes, constraints, defaults, stored procedures, triggers
  • Constraint: primary key, non-null, unique, default, check, foreign key
  • Foreign Key: Table A decision table B, the primary key table Table A, Table B for the foreign key table, the foreign key in the foreign table
T-SQL database operations
  • Create a database
create database student
on
(
name="student",	//主文件的逻辑名
filename="C:/student.mdf",	//存储数据库主文件的地址
size=5mb,//数据库主文件大小
maxsize=80mb,//最大容量
filegrowth=10% //增长值,以原大小的10%增长
)
log on 
(
name="student_log",//日志文件的逻辑名
filename="C:/student.ldf",//日志文件的存放地址
size=2mb,//日志文件的大小
maxsize=5mb,//日志文件的容量最大值
filegrowth=1mb//增长值,以1mb为单位增长
);
  • Delete Database
drop database student
  • Detach the database
sp_detach_db student
  • Additional database
create database student
on
(
filename="C:\Student.mdf"
)
for attach
  • View database information stored procedure
exec sp_helpdb student
  • Create a table
use student//打开数据库
创建学生表
create table stuifo//表名
(
stuid int primary key,//学号,设为主键,int型
stuname varchar(10) not null,//姓名,非空,varchar(10)型
cid int    //班级,int型(做外键)
)

 - 创建班级表

create table classifo//表名
(
cid int primary key       //班级序号,设为主键,int型
)
  • Establish a foreign key
alter table stuifo
add constraint stuclassifo(外键名) 
foreign key(cid) references classifo (cid)
  • Delete table
drop table stuifo
  • Add Column
alter table stuifo
add stusex bit
  • Remove Columns
alter table stuifo
drop column stusex
  • Modifying the data type of the column
alter table student
alter column stuname char(10)//要修改的类型
  • Insert row (data (recording))
use student
insert stuifo values(1,'小红',2)
或
insert into stuifo values(1),('小红'),(2)
  • Modify the line (Data)
use student
update stuifo set stuid=1,cid=3 
where stuname='小红'//条件,当姓名为小红时执行
  • Delete Row (data)
删除学号为1的数据
delete stuifo 
where stuid=1
  • Empty row (data)
truncate table stuifo(含有外键不可执行)
Database - Data Types
char(n) varchar(n) varchar(max) text
nchar(n) nvarchar(n) nvarchar(max) ntext
bit binary varbinary varbinary(max)
tinyint smallint int bigint
numeric demical(p,s) smallmoney money
float(n) real datetime datetime2
smalldatetime date time
datetimeoffset timestamp sql_variant uniqueidentifier
xml cursor table
  • demical (p, s): p is the number of bits, s is a decimal
  • N represents the data type is unicode encoding, each character occupies one byte, if not non-unicode encoding table n, representing an alphanumeric character, Chinese character occupies two bytes.
  • Var data type is represented as a variable length, should keep "abc", char (5) stored as "abc (rear two spaces fill bits)", varchar (5) stored as "abc" (no fill bits) .

My public numbers: Germany notes show
the public number is used to summarize some study notes.

Here Insert Picture Description
My github blog:
Silly Hidekiyo https://oydq.github.io

Released five original articles · won praise 5 · Views 346

Guess you like

Origin blog.csdn.net/m0_46652631/article/details/105213161