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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.
