例1:建立数据库
查看现有数据库
show databases;
建立demo数据库
create database demo;
查看现有数据库
show databases;
删除数据库
drop database demo;
查看现有数据库
show databases;
例2:建立基本表
如果demo数据库未建立,则先建立数据库
create database demo;
不选择缺省数据库的情况下建立学生表
错误的create table Student (
错误的
Sno char(9) not null,
Sname char(8) not null,
Ssex char(2) not null default 男,
Sage tinyint(2),
Sdept char(2)
);
create table demo.Student (
Sno char(9) not null,
Sname char(8) not null,
Ssex char(2) not null default 男,
Sage tinyint(2),
Sdept char(2)
);
选择demo为缺省的数据库
Use demo;
不用再demo.Course
不用再demo.Course
建立课程表
create table Course (
Cno char(1) not null,
Cname char(12) not null,
Cpno char(1),
Ccredit tinyint(1)
);
不用再demo.SC
不用再demo.SC
建立学生选课表
create table SC (
Sno char(9),
Cno char(1),
Grade tinyint(3)
);
查看demo库中基本表的数量
show tables;
show tables from demo; (如果demo不是当前数据库)
例3:查看基本表的结构
show columns from student;
desc student;
show columns from course;
desc course;
show columns from sc;
desc sc;
例4:修改基本表的结构
向student表的最后中插入一个字段
alter table student add address varchar(64);
查看student表结构所发生的变化
desc student;
向student表的第1列前插入一个字段
alter table student
add IDNum char(18) not null first;
查看student表结构所发生的变化
desc student;
向student表的sage列后插入一个字段
alter table student
add birthday date after sage;
查看student表结构所发生的变化
desc student;
删除新增加的三个字段
alter table student drop IDNum;
alter table student drop address;
alter table student drop birthday;
查看student表结构所发生的变化
desc student;
将Sdept由char(2)修改为char(8)
alter table student change Sdept Sdept char(8);
查看student表结构所发生的变化
desc student;
将Sage由tinyint(2)修改为int(6),并不允许为空
alter table student change Sage Sage int(6) not null;
查看student表结构所发生的变化
desc student;
将Ssex由char(2)修改为int(1),缺省为0
alter table student change Ssex Ssex int(1) default 0;
查看student表结构所发生的变化
desc student;
将Sno改名为Snum,由char(9)修改为int(6),且为主键
alter table student change Sno Snum int(6) primary key;
查看student表结构所发生的变化
desc student;
(也可以先删除Sno,再增加Snum)
例5:删除基本表
删除Student表
drop table student;
查看demo库中基本表的数量
show tables;
删除Course表/SC表
drop table course;
原创力文档

文档评论(0)