SQLServer從入門基礎


1.數據庫管理工具

工具創建數據庫

1>登錄數據庫管理工具【Microsoft SQL Server Management Studio】

 

2>右鍵【新建數據庫】

 

 

3>數據數據庫名稱,點擊確定,就建立好了一個【MyDB】的數據庫

 

創建數據表

 

 

代碼創建數據庫,以及數據表

 

 代碼創建數據庫

use master--用系統數據庫
go
if exists(select * from sys.sysdatabases where name='MyDB')--查詢MyDB數據庫是否存在
begin
  select '該數據已經存在'
  drop database myDB--如果存在MyDB數據庫則drop掉數據庫
  end
else--如果不存在
begin 
 create database MyDB
 on primary
 (
 name='MyDB',
 filename='C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDB.mdf',
 size=5mb,
 maxsize=100mb,
 filegrowth=15%
 )
log on
(
name='MyDB_log',
 filename='C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDB_log.mdf',
 size=5mb,
 maxsize=20mb,
 filegrowth=15%
)
end

 

 代碼創建數據表

use MyDB
go
if exists(select * from sys.sysobjects where name='Users')
begin
     select '該表已經存在'
     drop table Users
end
else
begin
create table Users
(
    Id int not null identity(1,1) primary key,
    UserName nvarchar(50) not null,
)
end

簡單的查詢語句

下圖數數據表Users的數據值

 

下圖是各種簡單的查詢語句的使用

use MyDB
select * from Users
--where
select * from Users where Id=1
--不等於,!=或者<>
select * from Users where Id != 2
select * from Users where Id <> 2
--or
select * from Users where Id=1 or UserName='bol'
--and
select * from Users where Id=1 and UserName='col'

--like '%a%'前后模糊查詢,只要字母中包含o就行
select * from Users where UserName like '%o%'

--‘%o’前模糊,只要以a開頭就行,后面的不管
select * from Users where UserName like 'a%'

--‘o%’后模糊,只要以l結尾就行,前面不管
select * from Users where UserName like '%l'

--not,除了以a開頭的,其他啥都行
select * from Users where UserName not like 'a%'
--in,一次請求幾個數據
select * from Users where Id in(1,2,3)
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM