Mysql-笔记(更新到22-10-24)

发布于 2022-10-25  1060 次阅读


show databases; 查看所有表
use  <>; 进去数据库
show tables; 查询当前数据库的表
desc <表名>   查询当前表内数据



查询语句 
select * from 表名  查询表的全部数据

创建数据库
create  database d_sample;



创建表的数据
create table student(   
char(9) primary key,
varchar(10),
char(2),
date,
varchar(10),
varchar(8)
);

ALTER TABLE <要修改表的昵称>
ADD<新字段名><数据类型>(长度)<约束条件>
ADD INDEX<检索名>
DROP<字段名>OR<检索名>    删除字段
DROP TABLE <表名>   删除表

RENAME AS<新表名字> 
modify <要修改的字段名><新数据类型>


向表插入数据
insert into <表名>[字段名] values [数据]  
例子:
insert into student values
('2010502','网三','W','2000-01-05','25','50');

更新数据
update <表名> set <字段名> =<表达式> where <条件>
具体例子:
update student set age='21' where sanme="zhangsan02"
update student set score = 100;

删除数据
detele from <表名> [where <条件>]


约束
show create table student;
定义约束
PRIMARY KEY 约束
方式一:create table <表名>
字段1  primary key//第一种添加主键
字段2
字段n。。
constraint p字段名 primary key (字段名)//第二种添加主键 约束
)

方式二:alter table <表名> add constraint <约束名称><约束类型>(字段名)

删除约束:
alter table <表明>drop constraint <约束名称>//通用
alter table <表明>drop  primary key//主键的

unique 约束 //唯一
方式一:create table <表名>
字段1  unique//第一种添加唯一
字段2
字段n。。
constraint usocialno  unique (字段名)//第二种添加唯一 约束
)

方式二:alter table student add unique(sname);

删除约束:
alter table student drop index sname;
alter table student drop constraint sname;


check约束   用来确定数据范围的约束
constraint  cscore cherk(score between 0 and 100)

foreign key 约束 
外键约束 用来确保参照完整性
constraint <约束名> foreign key <字段名> references <表名><>


NOT NUll 约束

-----not null
非空约束
在创建表的时候 设置某些字段名为非空


DEFAULT 约束
默认值约束  为特定属性设置默认值
----- default 'XXXX' //方式1
alter table 语句//后面加