mysql 基本语法学习1(数据库、数据表、数据列的操作)

时间:2023-03-09 06:33:50
mysql 基本语法学习1(数据库、数据表、数据列的操作)

今天学习了一下mysql语法,并记录下来

1、mysql的数据库操作

/***1、操作数据库的语法 ***/

-- 1)显示所有数据库 --
show databases; -- 2)创建数据库 --
create database testdb; -- 3)删除数据库 --
drop database testdb; -- 4)使用数据库 --
use testdb; -- 5) 查询数据库下所有表 --
show tables;

2、mysql的数据表操作

/*** 2、操作数据表的语法 ***/

-- 1)创建新的数据表 --
create table Student(
Uid int auto_increment primary key, -- 自增Uid,主键
StuName varchar(20) not null,
StuAge int not null,
StuSex varchar(10) not null,
StuMeg varchar(100) null
); -- 2)使用旧表创建新表 --
create table Student1 like Student; -- 3)添加数据到数据表 --
insert into Student(StuName,StuAge,StuSex,StuMeg) values('张三',12,'男','性格开朗');
insert into Student(StuName,StuAge,StuSex,StuMeg) values('丽丝',14,'女','性格活泼');
insert into Student(StuName,StuAge,StuSex,StuMeg) values('王琦',15,'男','沉默寡言'); -- 4)修改数据表数据 --
update Student set StuName='张泉' where Uid=''; -- 5)查询数据表数据 --
select * from Student; -- 6) 删除数据表 --
drop table Student; -- 7)清空数据表数据(新增数据时,Uid从0开始) ---
truncate table Student; -- 8) 清空指定数据表数据(新增时,Uid不会从0开始)--
delete from Student where Uid='';

3、mysql的数据列操作

/***数据表的行、列操作 ***/

-- 1) 为数据表添加列名 ---
alter table Student add column StuClass varchar(20); -- 2) 为数据表删除列名 --
alter table Student drop column StuClass; -- 3) 修改列名的数据类型 --
alter table Student modify column StuClass int; -- 4) 同时修改列名的数据类型和名称 --
alter table Student change StuClass sClass varchar(30);

Ps:

学习网址:http://www.jb51.net/article/28288.htm