#查看包含哪些数据库
show databases;
#使用名为 test 的数据库
use test;
#最简单的(tinyint 默认是 3 个字节,但可以显式地指定为 1 个字节;unsigned 须跟在后面)
create table tb0 (
id int not null,
name char(20),
age tinyint(1) unsigned default 0
);
#带主键的
create table tb1 (
id int not null primary key,
name char(20)
);
#复合主键
create table tb2 (
id int not null,
name char(20),
primary key (id, name)
);
#带默认值的(check只是个摆设,mysql会直接忽略之)
create table tb3 (
id int not null default 0 primary key,
name char(20) default '1',
sex char(1) not null check(sex in ('M', 'F')),
age tinyint not null check(age >= 0 and age < 100)
);
#插入记录
insert into tb3(id,name,sex,age) values(1,"",'M',99);
insert into tb3 values(2,"haha","F",888);
insert into tb3 values(3,"bruce.yang","A",23);
insert into tb3(id,sex,age) values(4,'M',123);
#自增长主键(auto_increment 和 default 冲突)
create table tb4 (
id int not null auto_increment primary key,
name char(20) default 'hahaha'
);
#插入记录
insert into tb4(id,name) values(888,"huhuhu"); #记录id为888
insert into tb4(name) values('bruce.yang'); #记录id为889
insert into tb4(id,name) values(1,"hello"); #记录id为1
insert into tb4(name) values("xixi"); #记录id为890
#使用 test 数据库
use test;
#删除 test 数据库中得 tb0 表
drop table tb0;
#在删除表的过程中,如果删除一个不存在的表将会产生错误,
#这时在删除语句中加入 IF EXISTS 关键字,就可避免产生错误,具体格式如下:
drop table if exists tb0;
#注意:
#在执行 CREATE TABLE、ALTER TABLE 和 DROP TABLE 中的任何操作时,
#首先必须要选择好数据库,否则是无法对数据表进行操作的
#查看表结构
desc tb4;
#删除记录(id 为 890 的记录删除以后,新插入记录的 id 将会从 891 开始)
delete from tb4 where id=890;
#更新记录(主键字段的值也可以更新)
update tb4 set id=892 where id=891;
update tb4 set id=893,name="9lala" where id=892;
MySQL 复习笔记I
最新推荐文章于 2024-07-07 00:28:24 发布