1.创建数据库
(1) create database test;
2.查看数据库列表
(1) show databases;
3.删除数据库
(1) drop database test;
4.查看当前数据库
(1) select database();
5.使用‘test’数据库
(1) use test;
6.查看表结构
(1)desc student;
7.查看表列表
(1) show tables;
8.定义表
(1) 默认在当前数据库下定义表:create table student (
sno char(9) primary key,
sname char(20) unique,
ssex char(2),
sage smallint,
sdept char(20) );
(2) 在指定数据库中定义表:create table ssmdemo.student (
sno char(9) primary key,
sname char(20) unique,
ssex char(2),
sage smallint,
sdept char(20) );
(3) 加入完整性约束条件:create table course (
cno char(4) primary key,
cname char(40) not null,
cpno char(4),
ccredit smallint,
foreign key (cpno) references course(cno) );
(4) 主码由两个外键构成:create table sc (
sno char(9),
cno char(4),
grade smallint,
primary key (sno,cno),
foreign key (sno) references student(sno),
foreign key (cno) references course(cno) );
9.修改表
(1) 修改表名:alter table student rename to stu;
(2) 增加属性s_entrance:alter table student ADD s_entrance DATE;
(3) 修改属性类型:alter table student modify sage INT;
(4) 修改字段为not null:alter table student modify ssex char(2) not null;
10.删除表
(1) drop table student;
被引用时无法删除:ROR 3730 (HY000): Cannot drop table 'student' referenced by a foreign key constraint 'sc_ibfk_1' on table 'sc'.
需要先删除引用表或外键
11.取消已经部分输入的语句 \c
12.修改表,加入新字段
(1) alter table sc add Grade smallint;
13.添加字段id自增
create table user_info (
id smallint primary key auto_increment,
username varchar(255),
name varchar(20));
自增的字段必须设置为主键