Mysql数据库学习心得(3)
三.Mysql常识
(一)字段类型
1.INT[(M)]
正常大小整数类型
2.DOUBLE[(M,D)] [ZEROFILL]
正常大小(双精密)浮点数字类型
3.DATE
日期类型。支持的范围是'1000-01-01'到'9999-12-31'。MySQL以'YYYY-MM-DD'格式来显示DATE值,但是允许你使用字符串或数字把值赋给 DATE列
4.CHAR(M)
定长字符串类型,当存储时,总是是用空格填满右边到指定的长度
5.BLOB TEXT
BLOB或TEXT类型,最大长度为65535(2^16-1)个字符。
6.VARCHAR
变长字符串类型,最常用的类型。
(二)基本操作
1:显示数据库
mysql>SHOW DATABASES;
2:当前选择的数据库,
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
3.当前数据库包含的表信息:
mysql> SHOW TABLES;
+---------------------+
| Tables in test |
+---------------------+
| mytable1 |
| mytable2 |
+---------------------+
4.获取表结构
mysql> desc mytable1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| s1 | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5.创建表
表是数据库的最基本元素之一,表与表之间可以相互独立,也可以相互关联。创建表的基本语法如下:
create table table_name
(column_name datatype {identity |null|not null},
…)
其中参数table_name和column_name必须满足用户数据库中的识别器(identifier)的要求,参数datatype是一个标准的SQL类型或由用户数 据库提供的类型。用户要使用non-null从句为各字段输入数据。
create table还有一些其他选项,如创建临时表和使用select子句从其他的表中读取某些字段组成新表等。还有,在创建表是可用PRIMARY KEY、KEY、INDEX等标识符设定某些字段为主键或索引等。书写上要注意:在一对圆括号里的列出完整的字段清单。字段名间用逗号隔开 。字段名间的逗号后要加一个空格。最后一个字段名后不用逗号。所有的SQL陈述都以分号";"结束。
例:
mysql>CREATE TABLE guest (name varchar(10),sex varchar(2),age int(3),career varchar(10));
6.创建索引
索引用于对数据库的查询。一般数据库建有多种索引方案,每种方案都精于某一特定的查询类。索引可以加速对数据库的查询过程。创建 索引的基本语法如下:
create index index_name
on table_name (col_name[(length)],... )
例:
mysql> CREATE INDEX number ON guest (number(10));
7.执行查询
查询是使用最多的SQL命令。查询数据库需要凭借结构、索引和字段类型等因素。大多数数据库含有一个优化器(optimizer),把用户的查 询语句转换成可选的形式,以提高查询效率。
值得注意的是MySQL不支持SQL92标准的嵌套的where子句,即它只支持一个where子句。其基本语法如下:
SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY]
[DISTINCT | DISTINCTROW | ALL]
select_expression,...
[INTO {OUTFILE | DUMPFILE} 'file_name' export_options]
[FROM table_references
[WHERE where_definition]
[GROUP BY col_name,...]
[HAVING where_definition]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...]
[LIMIT [offset,] rows]
[PROCEDURE procedure_name] ]
其中where从句是定义选择标准的地方,where_definition可以有不同的格式,但都遵循下面的形式:
字段名操作表达式
字段名操作字段名
在第一种形式下,标准把字段的值与表达式进行比较;在第二种形式下,把两个字段的值进行比较。根据所比较的数据类型, search_condition中的操作可能选以下几种:
=检查是否相等
!=检查是否不等
> (或>=)检查左边值是否大于(或大于等于)右边值
[not] between检查左边值是否在某个范围内
[not] in检查左边是否某个特定集的成员
[not] like检查左边是否为右边的子串
is [not] null检查左边是否为空值
在这里,可以用通配符_代表任何一个字符,%代表任何字符串。使用关键字、和可以生成复杂的词,它们运行检查时使用 布尔表达式的多重标准集。
例:
mysql> select t1.name, t2.salary from employee AS t1, info AS t2swherest1.name = t2.name;
mysql> select college, region, seed from tournament
ORDER BY region, seed;
mysql> select col_name from tbl_nameswherescol_name > 0;
8.改变表结构
在数据库的使用过程中,有时需要改变它的表结构,包括改变字段名,甚至改变不同数据库字段间的关系。可以实现上述改变的命令是 alter,其基本语法如下:
alter table table_name alter_spec [, alter_spec ...]
例:
mysql> alter table dbname add column userid int(11) not null primary key auto_increment;
这样,就在表dbname中添加了一个字段userid,类型为int(11)。
9.修改表中数据
在使用数据库过程中,往往要修改其表中的数据,比如往表中添加新数据,删除表中原有数据,或对表中原有数据进行更改。它们的基本 语法如下:
数据添加:
insert [into] table_name [(column(s))]
values (expression(s))
例:
mysql>insertsintosmydatabase values('php','mysql','asp','sqlserver','jsp','oracle');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
