mysql数据库的创建、数据表的创建、修改、约束、查询

这篇博客详细介绍了MySQL数据库的操作,包括创建数据库、创建数据表、修改数据表结构、设置数据表约束(如默认值、非空、唯一键、主键、自动增加和外键)以及查询数据表的方法(全量查询、指定字段查询、去重查询、别名查询、四则运算、有条件查询和范围查询)。通过实例演示了每一步操作的具体SQL语句。

1. 创建数据库

database:数据库
创建数据库:create database database_name; (create 创建+database 数据库 + 要创建的数据库的名字
进入数据库:use database_name; (use+要进入的数据库的名字)
查看所有的数据库:show databases; (注意这是个固定的)
例如:
创建数据库t_student
进入数据库t_student
查看所有数据库

create database t_student; //创建数据库t_student
use t_student; //进入数据库t_student
show databases; //查看所有数据库

2 . 创建数据表

注意:创建数据表之前需要先进入一个数据库
创建数据表必须添加上每个表的属性

创建数据表:
create table + table_name(
字段名 + 数据类型定义,
字段名 + 数据类型定义,
字段名 + 数据类型定义);

例如:创建数据表t_student ,字段名stu_id 数据类型char长度为18,字段名stu_name 数据类型varchar长度为50

create table t_student(
stu_id char(18),
stu_name varchar50));//字符串经常发生变化就用varchar

查看数据表结构的命令:
desc + table_name;

例如:查看表t_student的结构

desc t_student; //查看数据表t-student的结构

3 . 修改数据表

3-1
修改数据表的名字:
alter table 旧名字 rename 新名字;

例如:修改数据表t_student的名字为t_student1

alter table t_student rename t_student1;

3-2
为数据表新添加一个字段:
alter table 表名 add 字段名 数据类型 (after 已存在字段名)
//表示添加新字段在一个字段之后

例如:在数据表t_student中新添一个字段名t_t 数据类型char 长度为5 在stu_name之后

alter table t_student add t_t char(5) after stu_name;

3-3
修改数据表中某一个字段的数据类型:
alter table 表名 modify 字段名 数据类型;

例如:把数据表t_student中的t_t 的数据类型由char(5) 修改为 int(5); ((注意:int 为整形))

alter table t_student modify t_t int(5);

3-4
修改数据表中某一个字段的字段名:
alter table 表名 change 旧字段名 新字段名 数据类型;

例如:把数据表t_student中的字段名t_t修改为stu_sex 数据类型为int(5)

alter table t_student change t_t stu_sex int5);//这里同时修改了字段名以及数据类型


3-5
修改数据表中字段的顺序:
alter table 表名 modify 字段名 数据类型 first/after 另一个字段名;(把字段调到另一个字段前面或后面)

例如:把表t_student中的stu_sex调到stu_name 前面

alter table t_student stu_sex int(5) first stu_name;

3-6
删除某个数据表中的某一个字段:
alter table 表名 drop 字段名;

例如:删除数据表t_student中的stu_sex

alter table t_student drop stu_sex;

3-7
复制同一个数据下的数据表:
create table 要创建的表名 like 被复制的表名;

例如:复制t_student的结构新建表t_student1;

create table t_student1 like t_student;

注意:如果当前在一个数据库中,要复制另一个数据库中的表格
create table 要创建的表名 like 另一个数据库的名字 被复制表名;

例如:在当前数据库中复制数据库db_sutdent中的数据表t_student的结构 新建表名为t_student1

create table t_student1 like db_student t_student;

3-8
删除数据表:
drop table 表名

例如:删除数据表t_student

drop table t_student;

4 . 数据表的约束

4-1 默认值
设置表格中字段名的默认值:alter table 表名 modify 字段名 数据类型 default ‘默认值’;(注意:设置中文需要引号)
删除默认值:alter table 表名 alter column 字段名 drop default;
例如:为表t_student中的stu_sex 设置默认为男,然后删除默认

alter table t_student modify stu_sex char(2) default '男';
alter table t_student alter column stu_sex drop default;

4-2 非空约束
设置字段非空约束:alter table 表名 modify 字段名 数据类型 not null;
删除非空约束:alter table 表名 modify 字段名 数据类型;

例如:设置表t_student中的stu_id不能为空,然后删除非空约束

alter table t_student modify stu_id char(15) not null;
alter table t_student modify stu_id char(15);

4-3 唯一键约束
设置唯一键:alter table 表名 modify 字段名 数据类型 unique;
删除唯一键:alter table 表名 drop index 字段名;

例如:设置表t_student中的stu_id 为唯一,然后删除唯一键约束

alter table t_studnet modify stu_id char(15) unique;
alter table t_student drop index stu_id;

4-4 主键约束
(注意:主键必须为非空约束,插入数据时设置主键的字段必须要有数据,not null 可以不写,主键默认为非空)
设置主键:alter table 表名 modify 字段名 字段类型 primary key;
删除主键:alter table 表名 drop primary key;

例如:设置表t_student中stu_id为主键,然后删除

alter table t_student modify stu_id char(15) primary key;
alter table t_student drop primary key;

4-5 自动增加
注意:设置自动增加必须设置主键或者唯一键约束,数据类型也必须要int

设置字段值自动增加: alter table 表名 modify 字段名 数据类型 主键或者唯一键 auto_increment;
删除自动增加: alter table 表名 modify 字段名 数据类型;

例如:给表t_student 的stu_id 设置为唯一键自动增加

alter table t_student modify stu_id int primary key auto_increment;
alter table t_student modify stu_id int;

4-6 外键(外键的数据类型必须与主键一致)

为已经创建的表设置外键:
alter table 表名 add constraint 自己想一个外键名 foreign key(要设置外键的字段名) references 另一个表名(另一个表主键名);
删除外键:
alter table 表名 drop foreign key 自己想的那个外键名;

例如:为表t_student的course_id 创建外键名为c_id并参照
表t_student 中的主键stu_id,然后删除

alter table t_student add constraint c_id foreign key(course_id) refenrences t_student(stu_id);
        //注意必须要设置主键stu_id而且数据类型要一致
alter table t_student drop foreign key c_id;
     //注意必须要自己设置一个外键名,不然要使用命令查看系统自动生成的外键名才能删除这个外键

5 . 数据表的查询

5-1 查询表中所有信息
select * from 表名;
“ * "表示所有

例如:
查询表t_student所有学生的信息

select * from t_student;

5-2 查询指定字段
select 字段名 from 表名;
如果要查询多条,就在字段名中间用”,“分开

例如:查询t_student中的stu_id

select stu_id from t_student;

5-3 distinct
查询不重复信息
select distinct 字段名 from 表名;

例如:查询t_student中stu_id没有重复记录

select distinct stu_id from t_student;

5-4 as
修改名称查询
select 字段名 as 你要修改的名字 from 表名;

例如:查询t_student中的stu_id 要求显示为学号

select stu_id as "学号" from t_student;

5-5 四则运算
"+",加
"-",减
"*",乘
”/”,除
"%",求余 --------例如5%2=1,5除以2余为1

select 字段名 运算名称 数字 from 表名;

例如:将成绩表t_score中的成绩显示都统一加十分

select grade + 10 from t_score;

5-6有条件的查询
select 字段名 from 表名 where 字段名 需要查询的条件;

例如:查询t_student中姓王 并且 性别为男的学生的全部信息

select * from t_student where stu_name like "王%" and stu_sex = "男";

like:大致查询,就大致的匹配
=:精确查询,就如题中性别为男,不包含其他字
%: 王%首字为王,%王%表示要查询当中含有王字

另:
多条件:字段名之间用","隔开,条件表“与”就用and,表“或”就用or,表示“非”就用!

5-7 范围查询
between …and…
select 字段名 from 表名 where between 一个条件 and 另一个条件; //查询某字段在什么范围之间

例如:查询学生生日在1999-2-1至1999–9-1之间

select stu_birth from t_student where between "1999-2-1" and "1999-9-1";

如果查询不在之间就在where后面加上not

6 . 数据表的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值