数据库初识--从MySQL 出发_MySQL
要学Web 开发,也得先对数据库有所了解呀。数据库分门别类,多种多样,目前我选择了 MySQL 。
看了 MySQL,觉得数据库处理也不是很难,主要就是一些对数据的处理,MySQL 主要就是一些命令的运用。
看过了,怕忘了吧,得及时记录下来。
数据库基础
(1)数据模型--有三类
层次模型 :用树形结构来表示实体及其之间的模型。
网状模型: 以实体型为节点的有向图表示各实体及其之间联系的模型。
关系模型: 用二维表格表示实体及其相互关系的模型。
前两模型较不常见,所以主要考虑 关系模型。
(2)关系数据库
关系是一个带有属性的表,这个表称为关系表,一组表组成一 数据库。
表由行和列组成,其行称为元组,列称为属性。
属性的取值范围称为域,行称为记录。
(3)键的选用
主键(Primary Key) 组合键 (Concatenated Key) 候选键 (Candidate Key) 外键 (Foreign Key)
这些键的具体区别我还是看不懂啊.....看来还要多理解理解.....
(4)关联类型
一对一 一对多 多对多
(5) 最后还有 第一二三范式 实体/参照/域完整性 主键/唯一/外键/默认约束 等等.......也不多说了..
********************************************华丽分割线************************************************************
MySQL 主要命令
(大小写不区分)
MySQL 的数值类型有:
整型: tinyint smallint mediumint bigint ; 浮点型 : float double decimal ;
MySQL 还有数据字段修饰符:
自动增量设置 auto_increment : create table book(id int auto_increment,name varchar(15));
然后插入数据 insert into book (id,name) values("1","book1"); insert into book(bame) values("book2"); 则 id 会自动自增。
自动补齐前导 0 zerofill : create table book(id int(3) zerofill,name char(20)); 再插入如上..id 就会变为 001 之类的.
另外还有两个类型: blob 和 text 通常保存较大文本,但前者可以保存二进制文件(如图片?)。
同样也有 tinyblob blob mediumblob longblob tinytext text mediumtext longtext
还有 enum 类型和 set 类型,恕我还没弄懂这俩货..暂时不说了...
还有那些跟时间日期相关的方法,规则也挺多,也跳过吧..
例如创建 books 数据库 :create database books;
查看数据库大纲信息:show databases;
创建新用户 user1并分配权限 select (他使用密码 "passwd",密码可选可不选) :
grant select on *.* to user1 identified by "passwd" with grant option;
查看用户权限: show grants for user1;
撤销用户权限:revoke select on *.* from user1;
要选择某数据库(books):use books; ----> (选择了才能操作它里面的表) 查看数据库表: show tables;
创建数据库表(book) : create table book(id int,name varchar(15),price varchar(10));
其中 前一项是属性,后一项是对应类型,varchar(15) 和char(15)差不多,只是后者固定了15个空间。
为表中插入值 :insert into book values(1 book1 10,2 book2 30);
查看某特定表(book)信息 : describe book;
删除数据库(books): drop database books ;
删除表数据(book) :delete from book where id="2"; 删除表中 id=2 的数据;
为某些值创建一视图(myView),就是某种意义上的“整合”:
create view myView as select id,name from book; 接下来 select * from myView; 就可以查看视图(也可用 describe myView;)
删除视图 drop myView;
查询语句+约束(where后面是约束): select * from book where name="book2";
(and or 等):select * from book where name="book2" and/or id="1";
(limit ) : select id from book order by id limit 3; 其中order by 可以id 方式来排序. 还可换成 limit 1,3; 代表区间1 ~ 3 ;
模糊查询(like):与通配符“%” 和下划线 “_"搭配使用,前者配一个或多个字符,后者只配一个字符。
如 select * from book where name like('_k%');
联合多列(concat) :就是把某些元素联合在一起:作为一个新的“列”?
如 select id,concat(name," ",price) as info from book; 就是 把 name 和 price 联合成 info 再与id 项 弄出来呗..
查询最大值最小值 : select max(id)/min(id) as id from book; 计算行数: select count(*) from book;
分组查询 (group by ): select * from book group by name ;
更新数据(更改数据 update ): update book set name="book111" where id="2"; 就是把id=2 的name 更改了。
等等等等........................
除此之外,最好为数据库创建索引,可以加快查询速度。
使用 alter table : alter table book add index/nique/rimary index_name; 其中三项为索引类型。
使用create index: create index index_name on book;
使用create table: create table book(index index_name);
查看(id)索引的使用:explain select id from book where name="book2";
还可以从宏观上查看 show status like "Handler_read%";
表的类型....
MyISAM (默认) HEAP(最快) 可直接如此定义 : create table book(id char)type = HEAP;
还有锁定表 READ 和 WRITE 外键 这三个还是看不大明 .....先不说了..
还有学会创建临时表 create temporary table book(id varchar(10) NOT NULL,); ps: NOT NULL 可以直接用, 你懂的吧....
分析表有 analyze table book; 检查表有 check table book ; 优化表有 optimize table book;
等等等等......
命令虽多,多打打,始终会熟悉的...
**************************************************************华丽的分割线****************************************************
第一篇博客就这样写完了,耗时将近两个钟头,而且是熬夜写...........精神可嘉..................
第一次写博文,格式什么的都不熟悉
以后不能熬夜了.......

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



Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

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.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
