MySQL基础教程中篇 (表操作教程)

欢迎观看lowkey的博客!

接着上篇,今天我们根据一个例子来学习MySQL的表的制作.

案例:写一个(老师,学生,班级,课程)的案例,分析其中的关系.

先介绍一下表的结构:

1.创建表:

create table tablename(

    列名    数据类型    约束条件,

    列名    数据类型    约束条件,

    列名    数据类型    约束条件,   ........

)charset=utf8;                                 指定表内容字符编码集。

2.数据类型:

int(n<11)                                       定义一个n位的整数

varchar(n<60000)                       定义一个长度为n个字节的字符串类型

folat(n)                                           定义一个小数为n的浮点数

text()                                              定义一个text类型的数据

double(m,n)                                  定义一个长度为m,小数为n的双精度浮点数

3.约束选项(重点): 

 primary key                                            主键约束 该列将作为主键使用(该列的值不能重复)。
 unique                                                     唯一约束 允许 null重复出现。
 auto_increment                                    自增约束,当该列为整形时 值自增。
 foreign key                                             外键约束 主要用来实现表与表之间的关系。
 not null                                                    非空约束  该列内容不允许空值。

4.外键约束:

#外建在从表中定义,定义时需要注意:

外键列的数据类型必须和参照列的类型一致(数据长度、编码类型、数据类型)主表的参照列需要定义主键或者索引

constraint [约束名] foreign key(外键列名) references <主表名>(<参照列的名字>)

5.我用MySQL APP的制作了一张图片,可以让你们更清晰的观看其中的关系链接,

.

6.制作表(总共六张:四张数据表,两张外键链接表):

首先创建一个School库:

create database School;

use School;

学生表(Student):

create table Student(

idStudent int(11) not null auto_increment ,

StudentName varchar(45) not null ,

age int(11) not null,

sex blob not null,

cid int(11) default null,

primary key (idStudent))charset=utf8;

班级表(Class):

 create table Class(

idClass int(11) not null auto_increment,

ClassName varchar(45) not null,

primary key (idClass))charset=utf8;

老师表(Teacher):

create table Teacher(

idTeacher int(11) not null auto_increment,

Title varchar(45) not null,

Name varchar(45) not null,

sex tinyint(1) default 0,

age int(4) default 0,

primary key (idTeacher))charset=utf8;

课程表(Course):

create table Course(

idCourse int(11) not null auto_increment,

CourseName varchar(45) not null,

CourseTime int(11) default null, 

CourseScore int(11) default null,

primary key (idCourse))charset=utf8;

学生与课程(Stu_Cour):

 create table Stu_Cour(

Stu_id int(11),

Cou_id int(11),

score double(4,2) default 0.00,

key fk_Stu_Cour_1_idx (Cou_id),

key fk_Stu_Cour_2 (Stu_id),

constraint fk_Stu_Cour_1 foreign key (Cou_id) references Course (idCourse),

constraint fk_Stu_Cour_2 foreign key (Stu_id) references Student (idStudent))charset=utf8;

老师与课程(Tea_Cour):

reate table Tea_Cour(

idTea int(11) not null,

idCou int(11) not null,

primary key (idTea,idCou),

key idCou (idCou),

constraint Tea_Cour_ibfk_1 foreign key (idTea) references Teacher (idTeacher),

constraint Tea_Cour_ibfk_2 foreign key (idCou) references Course (idCourse))charset=utf8;

表我们建好了,如果你是每个单词亲自动手敲一遍,那么你已经成功了一半,因为在我们敲的过程中难免会出错,而你在找错误的过程中会更加的记忆犹新,对语句的记忆和语法更好更熟练的掌握.

接下来,我们就要在表中添加数据:

7.添加数据有两种形势(),

形式一:

insert into <tablename> (列明1,列明2,....) values(value1,value2,.....),(value1,value2,.....)....;注意:value与列的对应关系,外键约束关系,字段约束。

insert into Student(idStudent,StudentName,age,sex,cid) values (1,'Tom',20,0,1),(2,'Kelly',18,1,3),(3,'Jack',21,0,2);

形式二:

insert into <tablename> values(value1,value2,.....);注意:不推荐该形式,value和列进行对应,和定义的顺序有关。

 insert into Teacher values (1,'newteacher','Dive',0,20),(2,'goldteacher','Mark',0,25),(3,'oldteacher','Adren',1,35);

8.查询数据:

select * from <tablename>;  (  *  代表查看所有,如果查看某一列,将  *  换成列名就ok)

命令:

show create table <tablename>; 查看表结构信息

desc <tablename>; 查看表结构

9.更新表数据:

update <tablename> set 列明=value,列明2=value2,.... where 列明=列值#更新指定列的值等于列值的数据的指定列 注意where 子句的限制.

10.删除表数据:

delete from <tablename> where 条件 #删除指定表中的符合where条件的数据行

11.清除表内容:

形式一:

delete from <tablename>;

形式二:

truncate table <tablename>;

两条命令的区别点:形式一不重置 自增属性 .
 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值