數據庫常識

1.登錄數據庫:mysql -h 127.0.0.1 -P 3306 -uroot -p
簡寫為 mysql -uroot -p
説明:
-h 後面是主機名(ip)
-P 後面是端口號
-u 後面是登錄的用户名
-p 後面是登錄密碼,如果不填寫,回車之後,會提示輸入密碼

2.輸入錯誤內容不想讓服務端執行
錯誤命令 \c

3.修改密碼:
mysqladmin -uroot -p原密碼 password 新密碼

4.登出(退出)數據庫:
exit或quit或Ctrl+d

數據庫基本操作

庫(文件夾)的增刪改查

# 增
create database db1;
create database user1 charset=utf8;
# 查
show databases;    #查看當前所有數據庫
show create database db1;	#查看單個
# 改
alter database db1 charset=utf8;
# 刪
drop database user1;

表(文件)的增刪改查

'''
在操作表時需要指定所在的庫
'''
# 查看當前所在的庫
select database();
# 切換庫
use db1;

# 查看當前庫下所有的表格
show tables;
# 增
create table teacher(id int,name varchar(10),height double); #創建表格
alter table teacher add sex enum('男','女');  #增加字段
create table db2.t1(id int)  # 操作其他庫的表格
# alter table 表名 change 原列名 新名 類型 約束;
alter table teacher change height heigh decimal(4,3) not null;

# 查
show create table student;
describe student;   #簡寫desc student 

# 改
alter table teacher modify name varchar(15) not null;   

# 刪
drop table teacher;
# alter table 表名 drop 列名;
alter table teacher drop height;

數據的增刪改查

'''
先有庫和表,在表下操作數據
'''

# 增
insert into teacher values(1,'大娃',1.65,'男');     # into可省略
insert into teahcer(name,height) values('二娃',1.75);
insert into teacher(name,height) values('三娃',1.70),('四娃',1.80); 
insert into teacher values(5,'五娃',1.70,'女'),(6,'六娃',1.80,'男'); 

# 查
select * from teacher;	# 查看此表中所有數據,數據量特別大時不建議用*
select name from teacher;  # 查看某一字段的內容
# 改
update teacher set sex ='女' where heigh >1.7;
# 刪
delete from teacher where id>4;
delete from teacher; # 刪除表中所有數據

navicat操作

新建數據庫


新建表

新建表/設計表


添加數據