Home Database Mysql Tutorial 第十三天3月7日之MySQL基础_MySQL

第十三天3月7日之MySQL基础_MySQL

Jun 01, 2016 pm 01:18 PM

bitsCN.com 

一、SQL

SQL:Structured Query Language的缩写

结构化查询语言

SQL工业标准:由ANSI(ISO(国际标准委员会international standard orgnation)核心成员)

按照工业标准编写的SQL能运行在任何数据库上。

方言:只能运行在特定数据库上的SQL语句叫做方言。

工业标准和方言:普通话和浙江话。

二、SQL语言的组成:

DDL:数据定义语言 Data Definition Language

DML:数据操作语言 Data Manipulation Language

DQL:数据查询语言 Data Query Language

TPL:事务处理语言 Transaction Process Language

DCL:数据控制语言

CCL:指针控制语言

三、基本知识

Java定义的类---------------表的结构

Java对象-------------------表中的记录

MySQL:非windows环境下是严格区分大小写的。

约定:关键字都采用小写。多个单词组成用_分割。

四、DDL

作用:定义数据库和表结构等的语句。

关键字:create alter drop truncate(摧毁)

练习:

1、DDL之数据库操作

创建一个名称为mydb1的数据库。

mysql>create database MYDB1;

查看有哪些数据库

mysql>show databases;

查看创建数据库的具体细节

mysql>show create database MYDB1;

创建一个使用gbk字符集的mydb2数据库。

mysql>create database MYDB2 characterset gbk;

创建一个使用gbk字符集,并带校对规则的mydb3数据库。

mysql>create database MYDB3 characterset gbk collate gbk_chinese_ci;

删除前面创建的mydb3数据库

mysql>drop database MYDB3;

查看服务器中的数据库,并把mydb2的字符集修改为utf8;

mysql>alter database MYDB2 character setutf8;

2、DDL之表结构

选择一个库

mysql>use MYDB1;

创建一个员工表

mysql>create table EMPLOYEE(

IDint,

NAMEvarchar(200),

GENDERvarchar(200),

BIRTHDAYdate,

ENTRY_DATEdate,

JOBvarchar(200),

SALARYfloat(8,2),

RESUMEtext

);

显示当前库中的所有表

mysql>show tables;

查看表的结构

mysql>desc EMPLOYEE;

查看表的创建细节

mysql>show create table EMPLOYEE;

在上面员工表的基本上增加一个image列。

mysql>alter table EMPLOYEE add IMAGEblob;

修改job列,使其长度为60。

mysql>alter table EMPLOYEE modify JOBvarchar(60);

删除image列。

mysql>alter table EMPLOYEE drop IMAGE;

表名改为user。

mysql>rename table EMPLOYEE to USER;

修改表的字符集为utf8

mysql>alter table USER character setutf8;

列名name修改为username

mysql>alter table USER change NAMEUSERNAME varchar(100);

五、DML:Data ManipulationLanguage

用于向数据库表中插入、删除、修改数据

常用关键字:INSERT UPDATEDELETE

在MySQL中,字符串和日期时间类型,要使用单引号引起来。

特殊值:null

练习:

使用insert语句向USER表中插入三个员工的信息。

mysql>insert into USER(ID,USERNAME,GENDER,BIRTHDAY,ENTRY_DATE,JOB,SALARY,RESUME) values(1,'wangdongxue','female','1991-09-08','2014-02-17','CEO',100000,'beautygirl');

mysql>insert into USER values(2,'wangdong','male','1990-09-08','2014-02-17','CTO',100000,'strong man');

mysql>insert into USER values (3,'查显成','男性','1988-09-08','2014-02-17','UFO',100000,'帅锅');

查看表中的所有记录

mysql>select * from USER;

查看库的所有的编码:

mysql>show variables like'character_set%';

character_set_client:指示客户端使用的字符集

mysql>set character_set_client=gbk; 通知服务器,客户端使用的是GBK字符集

character_set_results:指示显示的结果集使用的字符集

mysql>setcharacter_set_results=gbk; 通知服务器,客户端接受的结果使用的字符集为gbk

将所有员工薪水修改为5000元。

mysql>update USER set SALARY=5000;

将姓名为’查显成’的员工薪水修改为3000元。

mysql>update USER set SALARY=3000 whereUSERNAME='查显成';

将姓名为’wangdong’的员工薪水修改为4000元,job改为CEO。

mysql>update USER setSALARY=4000,JOB='CEO' where USERNAME='wangdong';

将王冬雪的薪水在原有基础上增加1000元。

mysql>update USER set SALARY=SALARY+1000where USERNAME='wangdongxue';

删除表中名称为’查显成’的记录。

mysql>delete from USER where USERNAME='查显成';

删除表中所有记录。(DML语句)

mysql>delete from USER; #一条一条的删除

使用truncate删除表中记录。 (DDL语句)

mysql>truncate USER; #删除整张表,重建的表结构

六、DQL数据查询语言

查询

关键字:select

查询表中所有学生的姓名和对应的英语成绩。(投影查询)

mysql>select NAME,ENGLISH from STUDENT;

过滤表中重复数据。

mysql>select distinct ENGLISH fromSTUDENT;

在所有学生数学分数上加10分特长分。

mysql>select NAME,MATH+10 from STUDENT;

统计每个学生的总分。

mysql>select NAME,CHINESE+ENGLISH+MATHfrom STUDENT;

使用别名表示学生分数。

mysql>select NAME as姓名,CHINESE+ENGLISH+MATH总分 from STUDENT;

查询姓名为王五的学生成绩

mysql>select * from STUDENT where NAME='王五';

查询英语成绩大于90分的同学

mysql>select * from STUDENT whereENGLISH>90;

查询总分大于200分的所有同学

mysql>select * from STUDENT where(CHINESE+ENGLISH+MATH)>200;

模糊查询的pattern: _表示匹配一个字符 %匹配任意字符

查询英语分数在 80-90之间的同学。

mysql>select NAME,ENGLISH from STUDENTwhere ENGLISH between 80 and 90;

查询数学分数为89,90,91的同学。

mysql>select NAME,MATH from STUDENTwhere MATH in(89,90,91);

查询所有姓李的学生成绩。

mysql>select * from STUDENT where NAMElike '李%';

查询数学分>80,语文分>80的同学。

mysql>select * from STUDENT whereMATH>80 and CHINESE>80;

满足where条件的记录才会显示,逻辑运算的结果true。

对数学成绩排序后输出。

mysql>select NAME,MATH from STUDENTorder by MATH;

对总分排序后输出,然后再按从高到低的顺序输出

mysql>select NAME,CHINESE+MATH+ENGLISH总分 from STUDENT order by总分 desc;

注意:order是关键字。把关键字当做普通标示符对待,使用``(不是单引号,反引号)引起来

对姓李的数学学生成绩排序输出

mysql>select NAME,MATH from STUDENTwhere NAME like '李%' order by MATH desc;

分页查询

查询前3条记录

limit M,N; M开始记录的索引(从0开始),N一次要查询的条数

mysql>select * from STUDENT limit 0,3;

七、数据的完整性

数据完整性是为了保证插入到数据中的数据是正确的,它防止了用户可能的输入错误。

1、域完整性(列完整性)

指数据库表的列(即字段)必须符合某种特定的数据类型或约束

ID int(11):该字段必须是整数,长度不能超过11

NAME varchar(100) not null:不能为null

USERNAME varchar(100) unique:有的话必须唯一

2、实体完整性(记录完整性)

规定表的一行(即每一条记录)在表中是唯一的实体

主键约束:primary key

ID int primary key; ID就是主键(不能为null,而且是唯一的)

或者

ID int,

primary key(ID)

MySQL:主键可以自动增长。auto_increment(主键的值由MySQL数据来管理和维护)。oracle中没有自动增长。

create table T2(

ID int primary keyauto_increment,//auto_increment是方言,不是标准,只有MySQL中有。

NAME varchar(100)

);

3、参照完整性(引用完整性):外键 foreign key (多表情况)

一对多、多对多、一对一

remote:远程意思

collation校对意思

charset字符集意思

manipulation操作

MySQL:

时间戳:赋值了没用,不赋值就是把当前值给字段(数据库中)

clob(存放文本数据)能存的,bolb(存放二进制文件)就能存。

engine引擎:InnoDB:表示支持事务,安装时自己选择的

1.域完整性

not null 可以是空字符串,但不没能没有

unique可以为空,有的话必须唯一

2.order by后面的字段必须出现在select后面即投影字段的后面吗?不是必须的

+------------+--------------+------+-----+---------+-------+

| ID | int(11) | YES | |NULL | |

| NAME | varchar(200) | YES | | NULL | |

| GENDER | varchar(200) | YES | | NULL | |

| BIRTHDAY | date | YES | |NULL | |

| entry_date | date | YES | | NULL | |

| job | varchar(200) | YES | | NULL | |

| salary | float(8,2) | YES | |NULL | |

| resume | text | YES | | NULL | |

| IMAGE | blob | YES | |NULL | |

+------------+--------------+------+-----+---------+-------+

9 rows in set (0.01 sec)

mysql> alter table employee modify JOBvarchar(60);

Query OK, 0 rows affected (0.08 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc employee;

+------------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key |Default | Extra |

+------------+--------------+------+-----+---------+-------+

| ID | int(11) | YES | |NULL | |

| NAME | varchar(200) | YES | | NULL | |

| GENDER | varchar(200) | YES | | NULL | |

| BIRTHDAY | date | YES | |NULL | |

| entry_date | date | YES | | NULL | |

| JOB | varchar(60) | YES | |NULL | |

| salary | float(8,2) | YES | |NULL | |

| resume | text | YES | |NULL | |

| IMAGE | blob | YES | |NULL | |

+------------+--------------+------+-----+---------+-------+

9 rows in set (0.01 sec)

mysql> alter table employee

-> drop image;

Query OK, 0 rows affected (0.18 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc employee;

+------------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key |Default | Extra |

+------------+--------------+------+-----+---------+-------+

| ID | int(11) | YES | | NULL | |

| NAME | varchar(200) | YES | | NULL | |

| GENDER | varchar(200) | YES | | NULL | |

| BIRTHDAY | date | YES | |NULL | |

| entry_date | date | YES | | NULL | |

| JOB | varchar(60) | YES | |NULL | |

| salary | float(8,2) | YES | |NULL | |

| resume | text | YES | |NULL | |

+------------+--------------+------+-----+---------+-------+

8 rows in set (0.01 sec)

mysql> alter table employee

-> to USER;

ERROR 1064 (42000): You have an error inyour SQL syntax; check the manual that

corresponds to your MySQL server versionfor the right syntax to use near 'to US

ER' at line 2

mysql> rename table employee

-> to USER;

Query OK, 0 rows affected (0.15 sec)

mysql> show tables;

+-----------------+

| Tables_in_mydb1 |

+-----------------+

| user |

+-----------------+

1 row in set (0.00 sec)

mysql> show create table user;

+-------+-----------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

----+

| Table | Create Table

|

+-------+-----------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

----+

| user | CREATE TABLE `user` (

`ID` int(11) DEFAULT NULL,

`NAME` varchar(200) DEFAULT NULL,

`GENDER` varchar(200) DEFAULT NULL,

`BIRTHDAY` date DEFAULT NULL,

`entry_date` date DEFAULT NULL,

`JOB` varchar(60) DEFAULT NULL,

`salary` float(8,2) DEFAULT NULL,

`resume` text

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------+-----------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

----+

1 row in set (0.00 sec)

mysql> alter table user character setutf8;

Query OK, 0 rows affected (0.03 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table user change nameusername varchar(100);

Query OK, 0 rows affected (0.07 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into USER values(2,'wangdong','male','1990-09-08','2014-02-17','C

TO',100000,'strong man');

Query OK, 1 row affected (0.07 sec)

mysql> insert into USER values(1,'wangdongxue','male','1990-09-08','2014-02-17'

,'CTO',100000,'strong man');

Query OK, 1 row affected (0.05 sec)

mysql> insert into USER values(1,'wangdongxue','male','1990-09-08','2014-02-17'

,'CTO',100000,'strong man');

Query OK, 1 row affected (0.00 sec)

mysql> insert into USER values(1,'wangdongxue','male','1990-09-08','2014-02-17'

,'CTO',100000,'strong man');

Query OK, 1 row affected (0.07 sec)

mysql> select * from user;

+------+-------------+--------+------------+------------+------+-----------+----

--------+

| ID | username | GENDER |BIRTHDAY | entry_date | JOB | salary | res

ume |

+------+-------------+--------+------------+------------+------+-----------+----

--------+

| 2 | wangdong | male | 1990-09-08 | 2014-02-17 | CTO | 100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

| 1 | wangdongxue | male | 1990-09-08| 2014-02-17 | CTO | 100000.00 | str

ong man |

+------+-------------+--------+------------+------------+------+-----------+----

--------+

4 rows in set (0.06 sec)

mysql> insert into USER values (3,'查显成','男性','1988-09-08','2014-02-17','UFO

',100000,'帅锅');

ERROR 1366 (HY000): Incorrect string value:'/xB2/xE9/xCF/xD4/xB3/xC9' for colum

n 'username' at row 1

mysql> show variables like'character_set%';

+--------------------------+----------------------------------------------------

-----+

| Variable_name | Value

|

+--------------------------+----------------------------------------------------

-----+

| character_set_client | utf8

|

| character_set_connection | utf8

|

| character_set_database | utf8

|

| character_set_filesystem | binary

|

| character_set_results | utf8

|

| character_set_server | utf8

|

| character_set_system | utf8

|

| character_sets_dir | D:/Program Files/MySQL/MySQL Server5.1/share/chars

ets/ |

+--------------------------+----------------------------------------------------

-----+

8 rows in set (0.07 sec)

mysql> set character_set_client=gbk;

Query OK, 0 rows affected (0.07 sec)

mysql> select * from user;

+------+-------------+--------+------------+------------+------+-----------+----

--------+

| ID | username | GENDER |BIRTHDAY | entry_date | JOB | salary | res

ume |

+------+-------------+--------+------------+------------+------+-----------+----

--------+

| 2 | wangdong | male | 1990-09-08 | 2014-02-17 | CTO | 100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

+------+-------------+--------+------------+------------+------+-----------+----

--------+

4 rows in set (0.00 sec)

mysql> insert into USER values (3,'汤柳清','男性','1988-09-08','2014-02-17','UFO

',100000,'帅锅');

Query OK, 1 row affected (0.02 sec)

mysql> select * from user;

+------+-------------+--------+------------+------------+------+-----------+----

--------+

| ID | username | GENDER |BIRTHDAY | entry_date | JOB | salary | res

ume |

+------+-------------+--------+------------+------------+------+-----------+----

--------+

| 2 | wangdong | male | 1990-09-08 | 2014-02-17 | CTO | 100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO | 100000.00| str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

| 1 | wangdongxue | male |1990-09-08 | 2014-02-17 | CTO |100000.00 | str

ong man |

| 3 |

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

Video Face Swap

Video Face Swap

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

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)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

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.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

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.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

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.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

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: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

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.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

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.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

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.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

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.

See all articles