代表关键字:create(增)drop(删) alter(改)-- 增删改表的结构
1.语法:
create table tablebname(
columnname dateType(length),
columnname dateType(length),
columnname dateType(length),
columnname dateType(length),
columnname dateType(length)
);
语法:
mysql> create TABLE t_student(
-> no INT,
-> name VARCHAR(32),
-> sex CHAR(1),
-> birth DATE,
-> email VARCHAR(32)
-> );
将查询结果当做一张表创建,使用as关键字
语法:
create table emp1 as select * from emp;
create table emp2 as select empno,ename,sal from emp;
语法:
drop table t_student;
drop table if exists t_student;
删除表格:drop table t_student;
创建表格:
create table t_student(
no int(10),
name varchar(32)
);
查询表格:desc t_student;
给表格增加一个字段:alter table t_student add tel varchar(32);
给表格修改一个字段:alter table t_student modify tel varchar(20);
给表格删除一个字段:alter table t_student drop tel;
代表关键字:insert(增) delete(删) update(改)-- 增删改表中的数据
语法:
insert into tablename(columnname1,columnname2,columnname3,c...) values(value1,value2,value3,....)
字段和值必须一一对应,个数必须相同,数据类型必须一致
mysql> insert into t_student(no,name,sex,birth,email)
-> values(1,'zhangsan','m','1980-09-08','[email protected]');
mysql> insert into t_student(no,name,sex,birth,email)
-> values(1,'lisi','f','1970-09-08','[email protected]');
mysql> insert into t_student(no,name)
-> values(1,'kk');
如何插入中文数据:
使用DOS命令窗口不能直接插入中文,因为DOS窗口是GBK的编码方式,但是数据库表只接受UTF-8
因此可以使用图形化界面工具插入数据,在DOS窗口中查询的时候出现乱码,
可以修改查询结果集的显示编码方式。
mysql> set character_set_results ='GBK';
如何在创建表格的时候给字段添加默认值:
drop table if exists t_student;
create table t_student(
no int(10),
name varchar(32),
sex char(1) default 'm'
);
insert into t_student(no,name) values(1,'jack');
insert into t_student(no,name) values(2,'lucy');
select * from t_student;
将查询结果插入到一张表中:
insert into emp2 select * from emp2 where sal=3000;
语法:
update tablename set 字段名=字段值,字段名=字段值,字段名=字段值 where 条件
update t_student set name ='zhangsan',email='[email protected]' where no=3;
update t_student set name ='lisi';
update emp set ename = 'zhangsan' where ename like '%o%';
语法:
delete from tableame where 条件
mysql> delete from t_student where no=3;
delete from t_student;