简单的MySql入门sql语句

一 、创建数据库

create database (数据库名)

create databsae test4;

二、使用数据库

use (数据库名)

use test4;

三、展示数据表

show tables;

四、创建数据库表

create table (表名)

create table student(
 id int,
 name varchar(20)  
);

五、看数据表的结构

describe (表名)

describe student;

详细的表结构
shou full columns from (表名)

show full columns from student;;

六、查看数据库的编码

show variables likes ‘char%’;

七、查看表的数据

selec * from (表名);

select * from student;

八、插入表的数据

insert into (表名)(字段)values(字段值);

insert into student values(1,'zhangsan');

九、数据库退出

exit

十、停止运行数据库

net stop MySql;

十一、进行修改字段的字符编码

alter table (表名) modify (字段名) (字段类型) character set utf8 collate utf8_general_ci;

十二、查看某一个字段的字符编码

select charset (字段名) from (表名);

十三、更新数据

update set 字段名 新的字段值 where 字段名 字段原值;

update set name ='zhan' where name ='11';

十四、删除数据

delete from (表名) where (字段名) (字段值)

delete from student where name='11';

十五、常用数据表的操作

1、查询全部的数据表的数据
select * from 表名

select * from student;

2、在某个字段后面添加新的字段
comment: 备注
default null:默认值为0
alter table (表名) add (新字段)(字段的类型)default null comment ‘备注’ after (在某个字段的后面)

alter table student add disk varchar(20) default null comment '1' after name;

3、修改字段的类型
alter table (表名) modify column (需要改的字段) (新的字段类型)default null;

alter table student modify  column name int default null;

在正常的情况下,name是varchar类型
4、修改字段的名称
alter table (表名) change 原字段 新字段 新的字段类型 设置默认值。

alter table student change name sname varchar(20) default null;

十六、mysql建表的约束

1、主键的约束
主键的约束它能够确定一个的表的一条数据,也就是我们可以通过对某个字段添加的约束,就可以使得该字段不重复且不为空
primary key
写法一:

create table student(
   id int primary key,
   name varchar(20);
);

写法二:

create table student(
  id int,
  name varchar(20),
  primary key(id)
);

写法三:

alter table student add primary key (id);

删除主键约束

alter table student drop primary key;

2、唯一约束
用来约束字段的值不为空且不可以重复
1、写法一

create table student(
    id int,
    name varchar(10),
    unique(name)

);

2、写法二
添加唯一约束

alter table student add unique (name);

3、写法三

create table student(
        id int,
        name varchar(20) unique
);

4、写法四

create table student(
 id int,
 name varchar(20),
 unique(name)
);

5、删除约束
index :以…位索引

alter table student drop index name;

3、非空约束

create table student(
id iint,
name varchar(20) not null
)

4、默认约束
就是我们插入字段的时候,如果没有赋值的话,数据就会自动生成默认好的默认值,当赋值的时候,默认值就会失效。

create table student(
id int,
name varchar(10),
age int default 10

);

5、外键约束
外键约束会涉及到两个表
分别位主表和副表,也可以称为父表和子表

create table student(
   id int,
   name varchar(20)
   class_id int,
   foreign key(class_id) references class(id)
);
create table class(
   id int,
   name varchar(20),
   
)

1、在主表中class中没有的数值数据,在副表中是不可以引用的,要不然会报错,
2、主表中的记录被副表的数据引用的时候,在主表中是不可以删除数据。要不然会报错。

十七、数据库设计的三大范式

第一范式
在数据库表的设计中,每一个字段都是不可分割的原子值,字段值还可以拆分的值,就是不满住第一范式

第二范式
在必须满足第一范式的情况下,除了主键外的每一列都必须满足依赖于主键,如果出现不依赖的情况下,只可能发生在联合主键的情况下。
第三范式
在必须满足第二范式的情况下,除了主键列外的其他之间不能有传递关系依赖。
综合练习

综合练习
create table student2(
      sno int primary key,
	  sname varchar(20) not null,
	  ssex varchar(10) not null,
	  sbrithday datetime,
	  class varchar(20)
	  
);
create table course(
      cno int primary key,
	  cname varchar(10) not null,
	  tno int  not null,
	  foreign key(tno) references teacher(tno)
);
create table teacher(
   tno int primary key,
   tname varchar(20) not null,
   tsex varchar(20) not null,
   tbirthday datetime,
   prof varchar(20) not null,
   dapart varchar(20) not null
);
create table score(
  sno int not null,
  cno int not null,
  degree decimal,
  foreign key(sno) references student2(sno),
  foreign key(cno) references course(cno),
  primary key (sno,cno)
 );

1、查询student表中的所有记录

select * from student;

2、distinct 选取所有重复的值
select distinct (所需查询的字段)from 表名

select distinct dapart from teacher;

3、查询成绩是60到80的记录
写法一

select * from score where degree between 60 and 80;

写法二

select * from score where degree >=60 and degree <=80;

4、查询score中成绩位85,87,90的记录

select * from score where degree in(85,87,90);

5、查询student中class编号为1或者ssex是‘nv’的记录

select * from student where class='1' or ssex='nv';

6、查询数据记录的降序
order by (根据啥字段来进行排序)desc
desc是降序
asc是升序

select * from score  order by  degree desc;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值