Java programmers good share of the SQL MySQL entry (a)

Good programmers Java Share MySQL 's SQL entry (a) Introduction: various versions of the database, there is a common language for managing data in the database, it is SQL, in this chapter we will learn basic SQL statements.

       

SQL Overview

Structured Query Language Structured Query Language, is a special-purpose programming language, is a database query and programming language for accessing data and query, update and manage a relational database system.

All major database systems have made some changes and extensions to SQL coding specification. So, in fact between the different SQL database systems can not be fully interoperable, but most are the same, later we learn that MySQL version, after the main master SQL syntax, you want to migrate to other databases is relatively easy.

Structured Query Language consists of six parts:

A: Data Query Language (DQL: Data Query Language):

Its statement, also called "data retrieval statement", to obtain data from the table, the application to determine how the data is given. Reserved words SELECT is DQL (but for all SQL) is the most used verbs, other DQL commonly reserved words have WHERE, ORDER BY, GROUP BY and HAVING. These DQL reserved word often used in conjunction with other types of SQL statements.

Two: Data Manipulation Language (DML: Data Manipulation Language):

Its verb statements include INSERT, UPDATE, and DELETE. They are used to add, modify, and delete rows in the table. Also called action query language.

III: Transaction Processing Language (TPL):

Its statement ensure that your tables are affected DML statements in a timely manner all the rows to be updated. TPL statements include BEGIN TRANSACTION, COMMIT, and ROLLBACK.

IV: Data Control Language (DCL):

It is licensed by the statement GRANT or REVOKE, determine individual users and groups of users access to database objects. Some RDBMS can be used to control access to GRANT or REVOKE form a column.

Five: Data Definition Language (DDL):

CREATE statement includes its verb and DROP. Create a new table or delete the table (CREAT TABLE or DROP TABLE) in the database; a table join indexes. DDL includes a number of reserved words related to obtaining data with people database directory. It is also part of the action query.

Six: Pointer Control Language (CCL):

It statements like DECLARE CURSOR, FETCH INTO and UPDATE WHERE CURRENT is used alone to form the one or more operations.

In this chapter we introduced are DDL and DML.


MySQL database operations

Create a database of basic syntax is:

create database database name;

You can also specify a default character set and collation for the database:

create database database name

default character set the character set collate collation;

Select Database

use database name;

Modify the character set and collation of the database:

alter database database name

default character set the character set collate collation;

Delete the database:

drop database database name;

When you delete a database to check whether there is:

database name drop database if exists;

Code Example:

1 .-- delete database

2.drop database if exists java1903;

3 .-- Create a database

4.create database java1903

5.default character set utf8mb4

6.collate utf8mb4_general_ci;

7 .-- Use Database

8.use java1903;


MySQL common data types

After creating a database, we need to build the table, we need to set the table to build the data type of the field, we start to understand common MySQL data types.

type name

Explanation

Storage requirements

TINYINT

Small integer

1 byte

SMALLINT

Small integer

2 Yu Festival

MEDIUMINT

Integer medium-sized

3 byte

INT (INTEGHR)

Integer normal size

4 bytes

BIGINT

Large integer

8 bytes

2) decimal type

type name

Explanation

Storage requirements

FLOAT

Single-precision floating-point number

4 bytes

DOUBLE

双精度浮点数

8 个字节

DECIMAL (M, D),DEC

压缩的“严格”定点数

M+2 个字节

3) 日期/时间类型

类型名称

日期格式

日期范围

存储需求

YEAR

YYYY

1901 ~ 2155

1 个字节

TIME

HH:MM:SS

-838:59:59 ~ 838:59:59

3 个字节

DATE

YYYY-MM-DD

1000-01-01 ~ 9999-12-3

3 个字节

DATETIME

YYYY-MM-DD HH:MM:SS

1000-01-01 00:00:00 ~ 9999-12-31 23:59:59

8 个字节

TIMESTAMP

YYYY-MM-DD HH:MM:SS

1980-01-01 00:00:01 UTC ~ 2040-01-19 03:14:07 UTC

4 个字节

3) 字符串类型

类型名称

说明

存储需求

CHAR(M)

固定长度非二进制字符串

M 字节,1<=M<=255

VARCHAR(M)

变长非二进制字符串

L+1字节,在此,L< = M和 1<=M<=255

TINYTEXT

非常小的非二进制字符串

L+1字节,在此,L<2^8

TEXT

小的非二进制字符串

L+2字节,在此,L<2^16

MEDIUMTEXT

中等大小的非二进制字符串

L+3字节,在此,L<2^24

LONGTEXT

大的非二进制字符串

L+4字节,在此,L<2^32

ENUM

枚举类型,只能有一个枚举字符串值

1或2个字节,取决于枚举值的数目 (最大值为65535)

SET

一个设置,字符串对象可以有零个或 多个SET成员

1、2、3、4或8个字节,取决于集合 成员的数量(最多64个成员)

4) 二进制类型

类型名称

说明

存储需求

BIT(M)

位字段类型

大约 (M+7)/8 字节

BINARY(M)

固定长度二进制字符串

M 字节

VARBINARY (M)

可变长度二进制字符串

M+1 字节

TINYBLOB (M)

非常小的BLOB

L+1 字节,在此,L<2^8

BLOB (M)

小 BLOB

L+2 字节,在此,L<2^16

MEDIUMBLOB (M)

中等大小的BLOB

L+3 字节,在此,L<2^24

LONGBLOB (M)

非常大的BLOB

L+4 字节,在此,L<2^32

表的操作

创建表:

create table 表名

(

字段名 数据类型 [约束],

字段名 数据类型 [约束],

....

)

主要的约束类型有:

primary key                主键,表中只能有一个,不能重复,不能为空

not null                  非空,必须填写

unique                        唯一,不能重复

auto_increment        自动增长,必须是整数类型,不需要手动插入

foreign key                外键,建立表之间的引用关系

删除表:

drop table 表名;

删除表时进行检查:

drop table if exists 表名;

修改表,添加字段:

alter table 表名 add column 字段名 数据类型;

修改表,删除字段:

alter table 表名 drop column 字段名;

查看表结构:

desc 表名;


代码示例:

9.-- 删除表

10.drop table if exists tb_student;

11.-- 创建学生表

12.create table tb_student

13.(

14.        stu_id int primary key auto_increment,

15.        stu_name varchar(20) not null,

16.        stu_age int not null,

17.        stu_gender varchar(1) not null,

18.        stu_address varchar(200)

19.);



数据操作语言DML

数据操作语言有插入、删除和更新语句组成。

单行插入:

insert into 表名(字段名,字段名,字段名..) values(值,值,值..);

多行插入

insert into 表名(字段名,字段名,字段名..)

values(值,值,值..),(值,值,值..),(值,值,值..);

将一张表数据插入另一张表

insert into 表1(字段名,字段名,字段名..)

select 字段名,字段名,字段名 from 表2;

删除所有数据

delete from 表名;

清空表

truncate table 表名;

带条件的删除

delete from 表名 [where 条件];

更新

update 表名 set 字段 = 值,字段 = 值... [where 条件];

代码示例:

20.-- 插入一行学生记录

21.insert into tb_student(stu_name,stu_age,stu_gender,stu_address)

22.values('赵六',30,'男','上海');

23.-- 插入多行学生

24.insert into tb_student(stu_name,stu_age,stu_gender,stu_address)

25.values('陈七',20,'男','武汉'),('陈大七',28,'男','上海'),('陈小七',18,'男','北京');

26.-- 删除学号为3的学生

27.delete from tb_student where stu_id = 3;

28.-- 全部删除

29.delete from tb_student;

30.-- 清空表

31.truncate table tb_student;

32.-- 更新陈七的年龄为23,性别为女

33.update tb_student set stu_age = 23,stu_gender = '女'

34.where stu_name = '陈七';


总结

本章我们学习了SQL语言中的DDL和DML,能实现建表建表和数据的增删改操作,还有一个重要的查询操作,也就是DQL,会在下章介绍。


Guess you like

Origin blog.51cto.com/14249543/2402164