create table employee (
id int primary key not null auto_increment ,
name varchar(40) unique not null,
gender varchar(10) not null,
birthday date not null,
entry_date date not null,
job varchar(20) not null,
salary double not null,
resume longtext not null
);
3、数据表修改
向已有数据表添加一列 :alter table 表名 add 列名 类型(长度) 约束;
改变已有数据表一列类型、长度: alter table 表名 modify 列名 类型(长度) 约束;
改变已有数据表一列的名称 : alter table 表名 change 旧列名 新列名 类型(长度) 约束;
删除已有一列 : alter table 表名 drop 列名;
修改表名: rename table 旧表名 to 新表名;
修改表的字符集:alter table student character set utf8;
练习
在上面员工表的基本上增加一个image列 ----- alter table employee add image varchar(255);
修改job列,使其长度为60 ------ alter table employee modify job varchar(60) not null;
删除gender列。------ alter table employee drop gender;
表名改为user。 ----- rename table employee to user;
修改表的字符集为utf8 ---- alter table user character set utf8;
* show create table user;
列名name修改为username ---- alter table user change name username varchar(40) unique not null;
4、数据表删除
语法: drop table 表名;
* show tables; 查看当前数据 中所有表
步骤
1) 启动cmd窗口
2) mysql -u root -p 回车 输入密码
3) 创建数据库 day11 ------ create database day11 ;
* show databases ; 查看当前所有数据库
* show create database day11 ;
4) 创建数据表 ,先切换数据库 use day11;
* select database();
5) 创建数据表
create table employee (
id int primary key not null auto_increment ,
name varchar(40) unique not null,
gender varchar(10) not null,
birthday date not null,
entry_date date not null,
job varchar(20) not null,
salary double not null,
resume longtext not null
);
* desc employee; 查看表结构
6) 插入数据
insert into employee(id,name,gender,birthday, entry_date,job,salary,resume) values(null,‘zs‘,‘male‘,‘1990-01-10‘,‘2012-10-10‘,‘hr‘,3000,‘He is a good man!‘);
* 插入数据时,字符串添加 单引号 ‘‘ ---- 字符和日期型数据应包含在单引号中
insert into employee(id,name,gender,birthday, entry_date,job,salary,resume) values(null,‘lisi‘,‘male‘,‘1980-01-10‘,‘2000-10-10‘,‘manager‘,8000,‘He is a very good man!‘);
insert into employee values(null,‘wangwu‘,‘female‘,‘1977-10-08‘,‘1999-11-12‘,‘BOSS‘,100000,‘He is BOSS‘);
* 在插入数据时,如果有些列存在默认值或者可以为null ,插入省略部分列 。
create table person(
id int primary key not null auto_increment,
name varchar(40) not null,
introduce varchar(255)
);
insert into person(name) values(‘zs‘); --- 这里只要写不能为空列 就可以了
* 再插入语句时,省略所有列名
insert into person values(null,‘lisi‘,null); ---- 省略所有列名,必须为所有列提供value值,按照数据表中列顺序
* 插入数据后,通过select * from 表名; ------- 查询插入的数据
插入数据时,中文乱码问题
insert into employee values(null,‘小丽‘,‘female‘,‘1995-10-08‘,‘2015-11-12‘,‘Sales‘,2000,‘是一个有潜质的女孩子!‘);
* 查看数据库相关编码集 show variables like ‘character%‘;
* 使用mysql客户端 --- 黑色窗口界面使用gbk输入方式
mysql有六处使用了字符集,分别为:client 、connection、database、results、server 、system。
服务器端相关:database server system(永远无法修改 就是utf-8)
客户端相关 connection client results
解决插入乱码问题:将客户端相关三个编码集设置 gbk
set names gbk; ----- 快速设置客户端相关三个编码集 (临时设置当前窗口编码集)
练习
将所有员工薪水修改为5000元。 ------ update employee set salary = 5000 ;
将姓名为’zs’的员工薪水修改为3000元。update employee set salary = 3000 where name = ‘zs‘;
* update employee set salary = 3000 where binary name = ‘ZS‘; ---- 条件比较前添加 binary 使比较更加精确严格
将姓名为’lisi’的员工薪水修改为4000元,job改为ccc。 --------- update employee set salary = 4000, job=‘ccc‘ where binary name = ‘lisi‘;
将wangwu的薪水在原有基础上增加1000元。------- update employee set salary = salary+1000 where name = ‘wangwu‘;
3、数据表记录通过delete语句进行删除
语法:delete from 表名 where 条件语句;