all statements are; end
mysql keywords are not case sensitive
Learning ideas: view sql against visual history
Fixed syntax or keywords must be remembered
what statement is executed
View all databases
show databases;
switch to a database
use xxx;
View all tables in the database
show tables;
View all information in all tables in the database
describe 表名
--简写
desc 表名
create database
create database xxx;
exit the connection
exit;
Notes
--单行注释
/* */多行注释
CRUD: CRUD
DDL: Database Definition Language
DML: Database Query Language
DQL: Data Query Language
DCL: Database Control Language
Operating the database: (understand)
Operation database -> operation table -> data in operation table
Operating the database:
Create database: create database xxx (if already exists, fail)
create database [if not exists] xxx;//判断是否存在
Delete the database:
drop database [if exists] xxx;//如果存在
Using the database:
use xxx;
-- 如果表名或者字段名是一个特殊字符,就需要带``
View database
show database xxx;
The data (column) type of the database:
integer | ||
tinyint | small data | 1 byte |
smallint | smaller data | 2 bytes |
int | Standard integer (commonly used) | 4 bytes |
mediumint | medium size data | 3 bytes |
bigint | larger data | 8 bytes |
decimal | ||
float | single precision | 4 bytes |
double | Double precision (more common) | 8 bytes |
decimal | Floating point numbers in string form (to prevent loss of precision, financial calculations) | |
string | ||
char | fixed size string 0-255 | |
varchar | Variable length string 0-65535 (commonly used) | |
tinytext | Micro text 2^8-1 | |
text | Text string 2^16-1 | |
time and date | ||
date | YYYY-MM-DD | date |
time | HH:mm:ss | Time format |
datetime | YYYY-MM-DD HH:mm:ss | Most used |
timestamp | Timestamp 1970.1.1 milliseconds to the present | Commonly used |
year | year | |
null (don't use null for operations) |
Field properties of the database (emphasis)
unsigned | Unsigned integer, cannot be declared negative |
zerofill | 0-filled, the insufficient digits are filled with 0 int(3) 001 |
auto increment | Usually understood as self-increment, automatically +1 default on the basis of the previous record |
Usually used to design a unique primary key (index), which must be an integer type | |
The starting value and step size of the auto-increment of the primary key can be customized | |
not null NULLnotnull | not null: If you do not assign a value to it, an error will be reported NULL:如果不填写值,默认值就是null |
默认 | 设置默认的值 |
用sql语句创建表(重点):
在school数据库中创建一个学生表
create table if not exists `student`(
`id` int not null auto_increment comment'学号',
`name` varchar(30) not null default '匿名' comment'姓名',
`pwd` varchar(20) not null default '123456' comment'密码',
`sex` varchar(2) not null default'男' comment'性别',
`birthday` datetime default null comment'出生日期',
`address` varchar(100) default null comment'家庭住址',
`email` varchar(50) default null comment'邮箱',
primary key (`id`)
)engine=InnoDB default charset=utf8mb4
语法
create table [if not exists] `表名`(
`字段1` 数据类型 [not null] [default' '] [auto_increment] comment' ',
`字段1` 数据类型 [not null] [default' '] [auto_increment] comment' ',
`字段1` 数据类型 [not null] [default' '] [auto_increment] comment' ',
.....
`字段1` 数据类型 [not null] [default' '] [auto_increment] comment' ',
primary key(`主键字段名`)
)engin=innodb default charset=
注意:
使用英文括号,字段和字段名用``括起来,字符串用''括起来,所以的语句后面加英文逗号,最后一句不用加
primary key()设置主键,一般一个表只有一个唯一的主键。
charset设置数据库表的字符集编码
数据库引擎:(了解)
Innodb:默认使用
Myisam:早些年使用
myisam | innodb | |
事物支持 | 不支持 | 支持 |
数据行锁定 | 不支持(表锁) | 支持 |
外键约束 | 不支持 | 支持 |
全文索引 | 支持 | 不支持 |
表空间大小 | 较小 | 较大(2倍) |
MYISAM:节约空间,速度较快
INNODB:安全性高,事物处理,多表多用户操
索引的数据库文件都存在在data目录下,本质是文件存储,一个文件夹对应一个数据库
INNODB:在数据库表中只有一个.frm文件,以及上级目录下的ibdata1文件
MYISAM对应文件:*.frm文件(表结构的定义文件)*.MYD文件(数据文件)*.MYI 索引文件
修改表:
修改表名
alter table 表名1 rename as 表名2
增加表的字段
alter table 表 add 字段名 列属性
修改表的字段(重命名,修改约束):
alter table 表名 modify 字段名 属性 (修改约束)
alter table 表名 change 字段名 字段名1(字段重名名)
删除表的字段
alter table 表名 drop 字段名
删除表
drop table [if exists] 表
所有的创建和删除操作尽量加上判断,以免报错
注意:所有字段名使用``括号
注释:--
sql关键字大小写不敏感,建议小写
所有符号全部用英文
数据管理
外键(了解)
方式1:在创建时候创建外键约束
方式2:创建成功后添加外键约束
??????
删除有外键关系的表中,必选要先删除引用别人的表(从表),再删除主表
以上的操作是物理外键,数据库级别的外键,不建议使用
最佳实践:
●数据库就是单纯的表,只用来存数据,只有行(数据)和列(字段)
●想要使用外键,用程序去实现
DML语言
数据库意义:数据存储和数据管理
DML语言:数据库操作语言
insert:
语法:
insert into 表名([字段名1,字段2])values('值1'),('值2'),('值3')
如果不写表的字段,就会一一匹配。
一般写插入语句,数据和字段一定要一一对应。
insert into `student` (name,sex)
values("张三",'男'),
("李四","男");
*由于主键自增可以省略主键
*字段和字段之间使用逗号隔开。
*字段是可以省略,但是字段和值必须一一对应。
*可以同时插入多条语句,values后面的值用逗号隔开。