MySQl之数据库、表建立与数据的基本操作一

 

操作库:

数据库的建立:Create database 数据库名;          

显示库:Show databases;

删除库:Drop database 数据库名;

操作表:

       在操作表之前使用需要先确定使用那个数据库:use 数据库名

表的建立:

Create table 表名(

列名1 列类型<列的完整性约束>,

列名2 列类型<列的完整性约束>,

……

查看所有表:show tables;

显示表结构操控:desc 表名

修改表名:rename table 原表名 to 新表名;

       删除表:drop table 表名;

操作列:

追加列:alter table 表名 add 列名类型;

修改列类型:alter table 表名 modify 列名类型;

修改列:alter table 表名 changecolumn 原列名 新列名 类型;

删除列:alter table 表名 drop 列名;

操作数据(增删改)

插入:insert into 表名 (列名类型) values (值);

修改: update 表名 set 列名=值 where 条件语句;

删除:delete from 表名 where 条件语句;

操作数据库(查)

Distinct 为:过滤重复记录.

Select distinct 列名 from 表名

查询列名表达式:select 列名|表达式 from 表名;

表与表之间联系

即表与表之间通过主键与外键连接产生的联系

举例说明:

    两个表,一个Student表,一个Teacher表

          //创建学生表,定义学号,姓名

Create table student(

Id int(11) not nullauto_primary key,   //将学号id设为主键

Name varchar(20)

);

          //创建教师表,定义教师号,姓名

          Createtable teacher(

          Idint(11) not null auto_primary key,   //教师号id设为主键

          Namevarchar(20),

          Student_idint,   //建立外键,此时Student_id 就是Student表中的id 。

          ConstraintStudent_id_fk foreign key(Student_id) references Student(id)m   //将教师表中的Student_id 外键与Student表中的id主键联系起来

          );

          

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值