MYSQL数据库基本操作

MYSQL 数据库
https://www.cnblogs.com/wangxiaoxiaos/p/7662243.html

====================

 

 


mysql -h 主机名 -u 用户名 -p
密码
create database 数据库名[其他选项]
eg: create database `lc` character set utf-8;          # 数据库名称需要用键盘1左边的点号而不是引号
show databases; # 查看数据库
use lc; # 选择需要操作的数据库
create table 表名称(列声明);

# 以创建students表为例,表中将存放学号(id)、姓名(name)、性别(sex)、年龄(age)、联系电话(tel)这些内容:
eg:
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
# 针对表语句较长时,可以写在xxx.sql文件中,通过命令管道指向该脚本(导入单个.sql文件,无需登录MYSQL,直接在命令行下就可以)
eg: mysql -D lc -u root -p < d:/student.sql
admin

# 向表中插入数据
# insert into语句可以将一行或多行数据插入到数据库中,形式如下:
insert [into] 表名 [列名1,列名2,列名3,...] values(值1,值2,值3,...);
eg: insert into students values(NULL,"王刚","男",20,"13811371377"); # 其中[]内的内容是可选的

# 查询表中的数据
# select语句常用来根据一定的查询规则到数据库中获取数据,其基本用法为:
select 列名称 from 表名称 [查询条件]
eg: select name, age from students; # 查询students表中所有学生的名字和年龄
select * from students; # 使用通配符查询表中的内容

# 按特定条件查询
# where 关键字用于指定查询条件,用法形式为:
select 列名称 from 表名称 where 条件;
eg:
select * from students where sex="女"; # 查询性别为女的信息
select * from students where age>=21; # 查询年龄在21岁以上的所有人信息
select * from students where name like "%王%"; # 查询名字中带有"王"字的所有人信息

# 更新表中的数据
# update语句用来修改表中的数据,基本形式为:
update 表名称 set 列名称=新值 where 更新条件;
eg:
update students set tel=default where id = 2; # 将id为2的手机号改为默认

# 删除表中的数据
# delete 语句用于删除表中的数据,基本用法为:
delete from 表名称 where 删除条件;
delete from students where id=2;

# 创建后表的修改
# alter table 语句用于创建后对表的修改,用法如下:
# 添加列
alter table 表名 add 列名 列数据类型 [after 插入位置];
eg: alter table students add address char(60); # 在的最后追加列address
# 修改列
alter table 表名 change 列名称 列新名称 新数据类型;
eg: alter table students change tel telphone char(13) dafault "-"; # 将表tel列改名为telphone
# 删除列
alter table 表名 drop 列名称;
eg: alter table students drop birthday # 删除birthday列
# 重命名表
alter table 表名 rename 新表名;
eg: alter table students rename workmates; # 重命名students表为workmates

# 删除整张表
drop table 表名
eg: drop table workmates;

# 删除整个数据库
drop database 数据库名;
eg: drop database lc # 删除lc数据库

MYSQL数据库的数据类型:
MYSQL数据库三大数据类型,分别为数字、日期\时间、字符串,又可以细化为:

数字类型:
整数:tinyint、smallint、mediumint、int、bigint
浮点数:float、double、real、decimal
日期和时间:
date、time、datetime、timestamp、tear
字符串类型:
字符串:char、varchar
文本: tinytext、text、mediumtext、longtext
二进制(可用来存储图片、音乐等):tinyblob、blob、mdfiumblob、longblob

 

 

 

 备注:

mysql官网:https://dev.mysql.com/downloads/file/?id=508940
mysql安装教程:https://blog.csdn.net/weixin_42555080/article/details/87884902

常用的mysql数据库查看软件: dbForge Studio、navigate、pycharm企业版

 

posted @ 2022-01-12 15:58  宇宙刘  阅读(44)  评论(0)    收藏  举报