Home Database Mysql Tutorial 第二章 数据库的实现

第二章 数据库的实现

Jun 07, 2016 pm 05:43 PM
accomplish database

2.1 T-SQL语句回顾 在《使用SQL Server管理和查询数据》中,我们学习过T-SQL语句,它是Microsoft SQL Server 数据库对SQL语句的扩展,T-SQL语句的数据操作语法如下。 1. 添加数据 语法:Insert [into] 表名 (字段1,字段2,) Values (值1,值2,) 其中,

2.1 T-SQL语句回顾

  在《使用SQL Server管理和查询数据》中,我们学习过T-SQL语句,它是Microsoft SQL Server 数据库对SQL语句的扩展,T-SQL语句的数据操作语法如下。

  1. 添加数据

  语法:Insert [into] 表名 (字段1,字段2,···) Values (值1,值2,···)

  其中,Into 可选。

  例如: Insert into stuInfo (stuName, stuNo, stuAge) values ('张三', 's25301', 22)

  2. 修改数据

  语法:Update 表名 set 字段1 = 值1 , 字段2 = 值2,··· Where (条件)

  例如:Update StuInfo set stuAge = 24 where stuName = '张三'

  3. 查询数据

  语法:Select 字段1,字段2,··· from 表名 where (条件) Order by 字段名

  例如:Select stuName, stuNo from stuInfo where stuAge

  4. 删除数据

  语法:Delete from 表名 where (条件)

  例如:Delete from stuInfo where stuAge

  当项目经测试基本满足客户的需求后,需要部署在客户的实际环境中试运行。我们在部署前需要考虑的是,后台的数据库如何移植到客户的计算机中?考虑各种数据库版本的兼容性,行之有效的办法就是编写比较通用的SQL语句,包括创建库、创建表、添加约束、插入测试数据等。编写完毕后,存入*.sql文件中,最后复制到客户的计算机中,并执行*.sql文件中的SQL语句,从而实现后台数据库的移植。所以,我们还需要掌握如何使用SQL语句,实现创建库、创建表、添加约束和创建登录账户等。

2.2 使用SQL语句创建和删除数据库

  我们先简要回顾一下SQL Server数据库的基础知识。

  数据库文件由以下3部分组成。

  》》》主数据文件:*.mdf。

  》》》次要数据文件:*.ndf。

  》》》日志文件:*.ldf。

  其中,次要数据文件库可选,可以有多个数据库文件和日志文件。

  数据库文件的其他属性。

  》》》文件存放位置,分配的初始空间,属于哪个文件组。

  》》》文件的增长设置,可以按百分比或实际大小指定增长速度。

  》》》文件容量设置,网站空间,可以指定文件增长的最大值或不受限。

  其中,文件组允许对数据文件进行分组,以便于管理和数据的分配/放置。例如,可以分别在3个硬盘驱动器上创建3个文件(Data1.mdf,Data2.ndf和Data3.ndf),并将这3个文件指派到文件组fgroup1中。然后,可以明确地在文件组fgroup1中创建一个表。对表中数据的查询将分散到3个磁盘上,可以采用并发查询,因而性能得以提高。

2.2.1 创建数据表

  T-SQL创建数据库的语法如下:

  Creat Database 数据库名

    On [PRIMARY]

    (

    [,···n] []

    )

  [Log on]

  (

  {[,···n]}

  )

  文件的具体参数的语法如下。

  ([Name = 逻辑文件名,]

  FileName = 物理文件名

  [, Size = 大小]

  [, MaxSize = (最大容量 | Unlimited)]

  [, Filegrowth = 增长量]) [,···n]

  文件组参数的语法如下。

  Filegroup 文件组名 [,···n]

  其中,“[]”表示可选部分,“{}”表示必需的部分。各参数的含义说明如下。

  》》》数据库名:数据库的名称,最长为128个字符。

  》》》Primary:该选项是一个关键字,指定主文件组中的文件。

  》》》Log on:指明事务日志文件的明确定义。

  》》》Name:指定数据库的逻辑名称,这是在SQL Server系统中使用的名称,是数据库在SQL Server中的标识符。

  》》》FileName:指定数据库所在文件的操作系统文件名称和路径,该操作系统文件名和Name的逻辑名称一一对应。

  》》》Size:指定数据库的初始容量大小。

  》》》Maxsize:指定操作系统文件可以增长到的最大尺寸。

  》》》Filegrowth:指定文件每次增加容量的大小,当指定数据为0时,表示文件不增长。

  示例一:

  创建一个数据文件和一个日志文件。

  Create Database stuDB

  on primary   --默认就属于primary主文件组,可省略

  (

  /*-- 数据文件的具体描述 --*/

  Name = 'stuDB_data',   --主数据文件的逻辑名称

  filename = 'D:\stuDB_data.mdf',   --主数据文件的物理名称

  size = 5mb,   --主数据文件的初始大小

  Maxsize = 100mb,   --主数据文件增长的最大值

  Filegrowth = 15%,   --主数据文件的增长率

  )

  Log on

  (

  /*-- 日志文件的具体描述,各参数含义同上 --*/

  Name = 'stuDB_log',

  FileName = 'D:\stuDB_log.ldf',

  size = 2mb,

  Filegrowth = 1mb

  )

  go   --和后续的SQL语句分隔开

  在示例1中创建了学员信息数据库stuDB,该数据库的主数据文件逻辑名称为stuDB_data,物理文件名称为stuDB_data.mdf,初始大小为5MB,最大尺寸为100MB,增长速度为15%。数据库的日志文件逻辑名称为stuDB_log,物理文件名称为stuDB_log.ldf,初始大小为2MB,增长速度为1MB。该数据库存放在D:盘。

  示例二:

  创建多个数据文件和多个日志文件。

  Create DataBase employees

  on Primary

  (

  /*-- 主数据文件的具体描述 --*/

  Name = 'employees1',

  FileName = 'D:\employees1.mdf',

  Size = 10,

  Filegrowth = 10%,

  ),

  (

  /*-- 次要数据文件的具体描述 --*/

  Name = 'employee2',

  FileName = 'D:\employee2.ndf',

  Size = 20,

  MaxSize = 100,

  FileGrowth = 1

  )

  Log On

  (

  /*-- 日志文件1的具体描述 *--/

  Name = 'employeelog1',

  FileName = 'D:\employeelog1_log.ldf',

  Size = 10,

  MaxSize = 50,

  FileGrowth = 1

  )

  (

  /*-- 日志文件2的具体描述 *--/

  Name = 'employeelog2',

  FileName = 'D:\employeelog2_log.ldf',

  Size = 10,

  MaxSize = 50,

  FileGrowth = 1

  )

  go  -- 和后续的SQL语句分隔开

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

See all articles