day04【入门】MySQL学习(1)

发布于:2024-12-06 ⋅ 阅读:(99) ⋅ 点赞:(0)

目前的学习进度,如上图所示。从晚上开始学习MySQL数据库啦。


目录

1、数据库简介

2、数据集连接及准备工作

3、sql 语言中的注释

4、MySQL中常用数据类型

5、数据库中元素

6、创建表

7、insert插入记录

8、select查询

9、update修改数据

10、delete删除、truncate删除

11、删除表

12、字段的约束

(1)创建约束的语法:主键、自增长

(2)如果不指定字段,主键自增长字段的值可以用占位符,0或null

13、truncate与delete对自增长字段的影响区别

14、非空 not null

15、唯一

16、default 默认值约束


1、数据库简介

详细教程:MySQL 教程 | 菜鸟教程

2、数据集连接及准备工作

连接到mysql,然后创建数据库。新建查询

看到如下测试结果,则mysql设置成功:

3、sql 语言中的注释

单行注释:  快捷键  command+/

多行注释:  /*  注释内容 */

4、MySQL中常用数据类型

整数 int、小整数 tinyint(-128-127)、decimal 小数、varchar  字符

5、数据库中元素

database数据库、表 table、记录(行) record、字段(列) field、

6、创建表

create table a(
	name varchar(10)
);
create table b(
	name VARCHAR(10),
	height decimal(5,2)   # 5 表示位数,2表示小数位数
);
create table c(
	id int,
	name varchar(20),
	age tinyint UNSIGNED
);

7、insert插入记录

(1)

insert into c VALUES(
	0,'张三',23
);

(2) 

# 指定字段插入
insert into c(id,name) values(3,'曹操');

(3)

insert into c VALUES(5,'周瑜',50);
insert into c VALUES(6,'李四',78);

(4)

insert into c VALUES(10,'张1',12),
(11,'张2',13),
(12,'张5',14);

(5)

insert into c(id,name) VALUES(13,'孙悟空'),(14,'猪八戒'),(15,'唐僧');

8、select查询

(1)

select * from c;

(2)

select name,id FROM c;

select id,age from c;

select id,name from c;

9、update修改数据

(1)

update c set age=50;

update c set age=20,name='王欣欣' where id=1;

(2)update 其他例子

update c set name='李白' where name='曹操';

# id>10的记录,age+1
update c set age=age+1 where id > 10;

10、delete删除、truncate删除

(1)

delete FROM c WHERE id = 1;

delete from c;  # 删除所有记录

(2)

truncate table c;  # 删除所有记录

11、删除表

drop TABLE a;

DROP table if EXISTS a;  # 加个判断

12、字段的约束

(1)创建约束的语法:主键、自增长

create table a (
	id int UNSIGNED PRIMARY key,	
	name VARCHAR(10),
	age TINYINT
);

insert INTO a VALUES(1,'王一',18),(2,'王二',19),(3,'王三',20);

select * from a;
create table a (
	id int UNSIGNED PRIMARY key auto_increment,	
	name VARCHAR(10),
	age TINYINT
);

insert INTO a (name,age) VALUES('王一',18),('王二',19),('王三',20);

select * from a;

(2)如果不指定字段,主键自增长字段的值可以用占位符,0或null

# 如果不指定字段,主键自增长字段的值可以用占位符,0或null
insert into a VALUES(0,'孙多多',14);
insert into a VALUES(null,'李冬冬',18);

13、truncate与delete对自增长字段的影响区别

14、非空 not null

create table e(
	id int UNSIGNED, 
	name VARCHAR(10) NOT NULL,
	age int
)

15、唯一

create table a(
	id int UNSIGNED, 
	name VARCHAR(10) UNIQUE,
	age int
)

16、default 默认值约束

create table a(
	id int UNSIGNED, 
	name VARCHAR(10),
	age int DEFAULT 30
)

今天先学习到这块啦,明天尽量看完mysql相关的内容。~~~~~~


网站公告

今日签到

点亮在社区的每一天
去签到