How to build database and table in mysql

Database operation commands

This blog records the most basic operations of creating databases and data tables, teaches you how to get started with Mysql, basic database creation, precautions for data tables, and some taboos.
First of all, the database is case-insensitive, but it is very sensitive to the characters in the Chinese characters. Do not type the spaces randomly. Be sure to write punctuation in English.
You must enter the library before creating the table, otherwise the table cannot be created.
The default encoding format is utf-8, if the company has special requirements, it can also be changed.

Database foundation

Create a library: create database library name
Insert picture description here

Query database: show databases;
Insert picture description here

Delete library: drop database library name
Insert picture description here

To modify the library, only the encoding format can be modified: character set utf-8;
Insert picture description here

Query the currently used library: select database();
Insert picture description here

Switch database: use library name;
Insert picture description here

Create data table

Insert picture description here
Create table
create table table name (
column name data type (length) [constraint],
column name 2 data type (length) [constraint],
column name 3 data type (length) [constraint]
);

type of data:

Integer
int, fixed length 11
Floating point
double length (total length, including the number of decimals) (5,2) 999.99√ 10000.0× 1.1√
float
character type
char fixed-length character, length 0-255,'Zhang San'
varchar can be Variable-length character'Zhang San'
text mainly stores large text data
byte-type
blob byte type, suitable for storing images and other
date-type
date dates, no need to specify the length of
time, no need to specify the length of
datetime date and time, no need to specify the length
timestamp timestamp, the time will automatically change

constraint:

Constraints are constraints on column data.
The primary key constraint (primary key)
sets the column of the primary key , the value of which cannot be repeated, and cannot be empty.
The role of the primary key: the primary key is an index, and the efficiency of querying data through the primary key is extremely high. The
self-increment constraint (auto_increment)
auto-increment constraint. General cooperation used with primary key
Example: id int primary key auto_increment,
the only constraint (uNIQUE)
the column value is not repeated
Example: name varchar (10) unique
can not be empty (not null)
Example: id int not null,
a default value (default)
when When assigning a value, the default value will be assigned. For
example: id int default 0,
referential integrity constraint -> foreign key to
create a foreign key method
1 directly specify
in the table that references other table fields when creating the table Add a foreign key
constraint foreign key name foreign key (current table field) references other tables (fields)
2 After the table is built, specify the
alter table table name add constraint foreign key name foreign key (field) references other tables (fields)
examples

设计了外键后,课程表中的sid字段就与学生表的sid字段有联系.
父表: stu表 (被引用的表)
子表: course表 (引用表)
插入时:
	学生表可以随意插入值
	课程表插入值时,sid字段只能插入学生表中有的sid值
删除或更新时:
	RESTRICT: 如果想要删除/更新父表的记录,子表中有关联该父表记录的,则不允许删除父表记录
	NO ACTION: 同restrict一样,删除时先检查外键.如果有引用,不能删除
	CASCADE: 级联,父表删除/更新时,如果有子表有关联,那么子表父表数据一起删除/更新
    SET NULL: 置空,父表删除/更新时,如果子表有关联,那么子表该条记录置为null

View table field information
desc table name;
Insert picture description here
update table field-add column
alter table table name add column name data type (length);
Insert picture description here
update table field-delete column
alter table table name drop column name;
Insert picture description here
update column name
alter table table name change Old column name and new column name data type (length);
Insert picture description here
update table name
rename table old table name to new table name;
Insert picture description here
condition query:
selet field 1, field 2, field 3, ... from table name where condition;
fuzzy query:
select * (It can also be a field) from table name where field like% value %;

模糊匹配符号
		%,匹配任意个数的任意字符
		—_,匹配任意一个字符
例:-- 查询姓名以“张”开头的学生记录
select * from stu where sname like '张%';
	-- 查询姓名中包含“三”的学生记录
select * from stu where sname like '%三%';

Sort query:
select * from table name order by field sort type;

排序类型:
	升序  asc    
	降序	desc 
注意:如果后面省略了排序类型,默认就是升序
			order by 放在where 后面

Aggregate function :
Calculate multiple rows of data into one row. Return
syntax:
select aggregate function (field) from table name where condition;

Aggregate function:
count (field) count, calculate the number of data in the column that is not empty (null)
sum (field) sum the number of sums do not calculate the null value
avg (field) average, do not average
max (field) ) Maximum value
min (field) Minimum value
Features: the
aggregate function is placed between select and from.
Any field that appears with the aggregate function must appear after the group by

	--> 如果没有group by,那么普通字段就不能与聚合函数一起出现

======================================
what? Give the query field an alias, which can be used in the virtual table field
? how select field as an alias, as field aliases, ... from the table name
as commonly abbreviated
to expand Tidbits
view create table statements
show create table table name;
Insert picture description here
there please enlighten big brother

Guess you like

Origin blog.csdn.net/CV_Ming/article/details/112299189