1.数据库管理工具

工具创建数据库

1>登录数据库管理工具【Microsoft SQL Server Management Studio】

 

2>右键【新建数据库】

 

 

3>数据数据库名称,点击确定,就建立好了一个【MyDB】的数据库

 

创建数据表

 

 

代码创建数据库,以及数据表

 

 代码创建数据库

  1. use master--用系统数据库
  2. go
  3. if exists(select * from sys.sysdatabases where name=\'MyDB\')--查询MyDB数据库是否存在
  4. begin
  5. select \'该数据已经存在\'
  6. drop database myDB--如果存在MyDB数据库则drop掉数据库
  7. end
  8. else--如果不存在
  9. begin
  10. create database MyDB
  11. on primary
  12. (
  13. name=\'MyDB\',
  14. filename=\'C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDB.mdf\',
  15. size=5mb,
  16. maxsize=100mb,
  17. filegrowth=15%
  18. )
  19. log on
  20. (
  21. name=\'MyDB_log\',
  22. filename=\'C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDB_log.mdf\',
  23. size=5mb,
  24. maxsize=20mb,
  25. filegrowth=15%
  26. )
  27. end

 

 代码创建数据表

  1. use MyDB
  2. go
  3. if exists(select * from sys.sysobjects where name=\'Users\')
  4. begin
  5. select \'该表已经存在\'
  6. drop table Users
  7. end
  8. else
  9. begin
  10. create table Users
  11. (
  12. Id int not null identity(1,1) primary key,
  13. UserName nvarchar(50) not null,
  14. )
  15. end

简单的查询语句

下图数数据表Users的数据值

 

下图是各种简单的查询语句的使用

  1. use MyDB
  2. select * from Users
  3. --where
  4. select * from Users where Id=1
  5. --不等于,!=或者<>
  6. select * from Users where Id != 2
  7. select * from Users where Id <> 2
  8. --or
  9. select * from Users where Id=1 or UserName=\'bol\'
  10. --and
  11. select * from Users where Id=1 and UserName=\'col\'
  12.  
  13. --like \'%a%\'前后模糊查询,只要字母中包含o就行
  14. select * from Users where UserName like \'%o%\'
  15.  
  16. --‘%o’前模糊,只要以a开头就行,后面的不管
  17. select * from Users where UserName like \'a%\'
  18.  
  19. --‘o%’后模糊,只要以l结尾就行,前面不管
  20. select * from Users where UserName like \'%l\'
  21.  
  22. --not,除了以a开头的,其他啥都行
  23. select * from Users where UserName not like \'a%\'
  24. --in,一次请求几个数据
  25. select * from Users where Id in(1,2,3)

View Code

 

版权声明:本文为dfcq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dfcq/p/15181951.html