mysql,数据库和表的核心语句

复制代码
一.库操作

1.创建库
	create database if not exists 库名  default 字符集 default 校对规则

2.删除库
	drop database if exists 库名

3.修改库的,字符集,校对规则
	alter databse 库名  default 字符集 default 校对规则

4.查看当前使用的库
	seclect databse();

5.查看库
	show databases;

6.使用库
	use 库名;



二.表操作

1.创建表

	create table 库名.表名 (
	    字段 类型  约束,
	    ...
	    约束
	);

	约束:
	1.not null
	2.default:给字段设置默认值 default 'aaa'
	3.unique:该字段值唯一性
	4.primary key:主键,联合auto_increment使用
	5.auto_increment:自增
		(1)create table 库名.表名 (
		    	字段 类型  primary key auto_increment,
		    	...
		    	
		);

		(2)create table 库名.表名 (
		    	字段 类型,
		    	...
		    	 primary key(字段)
		);
	
		(3)添加删除主键

			alter table 库.表 add primary key(字段)
			alter table 库.表 drop primary key

		(4)复合主键:多个字段一起做主键,多个字段的值不完全相同

			create table 库名.表名 (
			    	字段1 类型,
				字段2 类型,
			    	...
			    	primary key(字段1,字段2)
			);

	5.foreign key:外键

		(1)create table db1.gz(
			字段 类型 约束,
			foreign key(字段) references 其它库.其它表(字段) 
			on update cascade  #级联更新
			on delete cascade  #级联删除
			);
	 	#级联更新, 当被引用的 yg 表中的 yg_id 列的值更新时,gz 表中的 gz_id 列的值也会相应地更新

		(2)删除外键
			show create table 库.表;

			alter table 库.表 drop foreign key 外键名;

		(3)添加外键
			alter table 库.表 
			add foreign key(字段) references 其它库.其它表(字段)
			on update cascade
			on detete cascade

2.查看库里面有哪些表
	show tables from 库名

3.查看表字段有哪些
	desc 库名.表名;

4.查看建库,建表的完整语句
	show create  database  库名
	show create  table     表名

5.修改表
	alter table 库名.表名 动作 操作

	动作:
	(1)rename:重命名表


	(2)drop:删除表字段


	(3)add:添加新字段
		alter table 表名 add 字段 字段类型 after 字段1,:在字段1后添加字段
		alter table 表名 add 字段 字段类型 first: 在最前面添加字段

	(4)change:修改字段名
		alter table 表名 change 旧字段名 新字段名 新数据类型;

	(5)modify:修改字段类型
		alter table 表名 modify 字段名 新数据类型;

6.复制表
	(1)select复制表结构和数据
		create table 库1.表1 select 字段 from 库2.表2 where 条件
	
	(2)like复制表结构
		create table 库1.表1 like 库2.表2

一.库操作

1.创建库

create database if not exists 库名 default 字符集 default 校对规则

2.删除库

drop database if exists 库名

3.修改库的,字符集,校对规则

alter databse 库名 default 字符集 default 校对规则

4.查看当前使用的库

seclect databse();

5.查看库

show databases;

6.使用库

use 库名;

二.表操作

1.创建表

create table 库名.表名 (

字段 类型 约束,

...

约束

);

约束:

1.not null

2.default:给字段设置默认值 default 'aaa'

3.unique:该字段值唯一性

4.primary key:主键,联合auto_increment使用

5.auto_increment:自增

(1)create table 库名.表名 (

字段 类型 primary key auto_increment,

...

);

(2)create table 库名.表名 (

字段 类型,

...

primary key(字段)

);

(3)添加删除主键

alter table 库.表 add primary key(字段)

alter table 库.表 drop primary key

(4)复合主键:多个字段一起做主键,多个字段的值不完全相同

create table 库名.表名 (

字段1 类型,

字段2 类型,

...

primary key(字段1,字段2)

);

5.foreign key:外键

(1)create table db1.gz(

字段 类型 约束,

foreign key(字段) references 其它库.其它表(字段)

on update cascade #级联更新

on delete cascade #级联删除

);

#级联更新, 当被引用的 yg 表中的 yg_id 列的值更新时,gz 表中的 gz_id 列的值也会相应地更新

(2)删除外键

show create table 库.表;

alter table 库.表 drop foreign key 外键名;

(3)添加外键

alter table 库.表

add foreign key(字段) references 其它库.其它表(字段)

on update cascade

on detete cascade

2.查看库里面有哪些表

show tables from 库名

3.查看表字段有哪些

desc 库名.表名;

4.查看建库,建表的完整语句

show create database 库名

show create table 表名

5.修改表

alter table 库名.表名 动作 操作

动作:

(1)rename:重命名表

(2)drop:删除表字段

(3)add:添加新字段

alter table 表名 add 字段 字段类型 after 字段1,:在字段1后添加字段

alter table 表名 add 字段 字段类型 first: 在最前面添加字段

(4)change:修改字段名

alter table 表名 change 旧字段名 新字段名 新数据类型;

(5)modify:修改字段类型

alter table 表名 modify 字段名 新数据类型;

6.复制表

(1)select复制表结构和数据

create table 库1.表1 select 字段 from 库2.表2 where 条件

(2)like复制表结构

create table 库1.表1 like 库2.表2

相关推荐
2501_945423541 小时前
用Matplotlib绘制专业图表:从基础到高级
jvm·数据库·python
2301_793804691 小时前
使用Python处理计算机图形学(PIL/Pillow)
jvm·数据库·python
哆啦A梦15888 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
Zzzzmo_8 小时前
【MySQL】JDBC(含settings.xml文件配置/配置国内镜像以及pom.xml文件修改)
数据库·mysql
FirstFrost --sy9 小时前
MySQL内置函数
数据库·mysql
2401_879693879 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
reembarkation9 小时前
光标在a-select,鼠标已经移出,下拉框跟随页面滚动
java·数据库·sql
eggwyw10 小时前
MySQL-练习-数据汇总-CASE WHEN
数据库·mysql
星轨zb10 小时前
通过实际demo掌握SpringSecurity+MP中的基本框架搭建
数据库·spring boot·spring security·mp
treacle田10 小时前
达梦数据库-配置本地守护进程dmwatcher服务-记录总结
数据库·达梦数据库·达梦数据库local数据守护