MySQL定义数据库和定义表

4.2定义数据库

4.2.1创建数据库

成功下载安装MySQL后在桌面“开始”找到MySQL文件夹单机运行。

输入初始设置密码,一般默认设置“123456”。(图示为登陆成功模式

 

【例 4.2】创建名称为stusys的学生信息数据库,在MySQL命令行客户端输入SQL语句:

mysql> create database stusys;

查看已有数据库:

mysql> show databases;

4.2.2选择数据库

【例 4.3】选择stusys为当前数据库。

mysql> use stusys;

4.2.3修改数据库

【例4.4】修改数据库stusys的默认字符集和校对规则

mysql> alter database stusys

    -> default character set gb2312

    -> default collate gb2312_chinese_ci;

4.2.4删除数据库

【例4.5】删除数据库stusys

mysql> drop database stusys;

查看现有数据库:

mysql> show databases;

4.5定义表

4.5.1创建表

创建新表
【例4.6】在学生信息数据库(stusys)中创建student表,student表是基本表之一。在MySQL命令行客户端输入如下SQL语句:

mysql> use stusys;

mysql> create table student

    -> (

    ->     sno char(6) not null primary key,

    ->     sname char(8) not null,

    ->     ssex char(2) not null default '男',

    ->     sbrithday date not null,

    ->     speciality char(12) null,

    ->     tc tinyint null

-> );

【例4.7】在数据库stusys中,使用复制方式创建student1表,表结构取自student表。

mysql> use stusys;

mysql> create table student1 like student;

4.5.2查看表

  1. 查看表的名称

【例4.8】查看数据库stusys中的所有表名。

mysql> use stusys;

mysql> show tables;

  1. 查看表的基本结构

【例4.9】查看数据库stusys中student表的基本结构。

mysql> show columns from student;

mysql> DESC student;

  1. 查看表的详细结构

【例4.10】查看数据库stusys中student表的详细结构。

mysql> show create table student\G

4.5.3修改表

添加列
【例4.11】在数据库stusys的student1表中增加一列sid,添加到表的第1列,不为空,取值唯一并自动增加。

mysql> alter table stusys.student1

    -> add column sid int not null unique auto_increment first;

使用DESC语句查看student1表。

mysql> DESC stusys.student1;

【例4.12】将数据库stusys的student1表的sbirthday列修改为sage,将数据类型改为tinyint,可为空,默认值为18。

mysql> alter table stusys.student1

    -> change column sbrithday sage tinyint default 18;

使用DESC语句查看student1表。

mysql> DESC stusys.student1;

  1. 删除列

【例4.13】删除数据库stusys的student1表中的sid列。

mysql> alter table stusys.student1

    -> drop column sid;

 

  1. 重命名表

【例4.14】在数据库stusys中,将student1表重命名为student2表。

mysql> alter table stusys.student1

    -> rename to stusys.student2;

【例4.15】在数据库stusys中,将student2表重命名为student3表。

mysql> rename table stusys.student2 to stusys.student3;

 

.5.4删除表

【例4.16】删除数据库stusys中的student3表。

mysql> drop table stusys.student3;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值