当前位置:网站首页>MySQL Basics

MySQL Basics

2022-06-10 20:46:00 Rulyc

1、 Get your own computer on the LAN IP Address ipconfig

SQL Grammatical norms :

  • 1、 One SQL Statement can span multiple lines , It ends with a semicolon
  • 2、 If there is a syntax error in a statement , Then this statement and all subsequent statements will not be executed
  • 3、SQL Commands are not case sensitive , Traditionally, database keywords are capitalized , Non key words in lowercase
  • 4、SQL The command can use a single line comment (#…) And multiline comments (//), The contents of the comment will not be executed by the server

2、 Common command :

show databases;  Displays all databases currently on the server 
quit;  Exit connection 
use  Database name ; Enter the specified database 
show tables;  Display all the data tables in the current database 
desc  The name of the table ;  Describe which columns are in the table ( Header )

3、 frequently-used SQL command
a、 Discard the specified database , If it exists

DROP DATABASE IF EXISTS  Database name ;

b、 create new database

CREATE DATABASE  Database name ;

c、 Enter the created database

USE  Database name ;

d、 Create a table to save data

CREATE TABLE  Table name (
 Field     type ,
 Field     type 
);

eg:

CREATE  TABLE  student(
    sid  INT,
    name  VARCHAR(8),
    sex  VARCHAR(1),
    score  INT
  );

e、 insert data

INSERT INTO  Table name  VALUES();

eg:

INSERT  INTO  student  VALUES('1','tom','M','87');

f、 Query data

SELECT * FROM  Table name ;

g、 Update data

UPDATE  Table name  SET   Field name 1 = ' Content ',  Field name 2=' Content ' WHERE id = '1';

eg:

UPDATE  user  SET
upwd=’888888’,phone=’19912345678’
WHERE uid=’1’;

h、 Delete data

DELETE FROM  Table name  WHERE  Conditions ;

eg:

DELETE  FROM  user  WHERE  uid='2';

4、 constraint
a、 Primary key constraint —— PRIMARY KEY
b、 Non empty constraint ——NOT NULL
c、 Unique constraint ——UNIQUE
d、 Default constraint ——DEFAULT
have access to DEFAULT Keyword declares the default value , There are two ways to use it

  INSERT INTO laptop VALUES(1, DEFAULT,);
  INSERT INTO laptop(lid,price) VALUES(2,3000);

Insert values for specific columns , All other columns use default values
e、 Check constraint —— CHECK
Check constraints can be customized to validate the inserted data

 CREATE  TABLE  student(
    score TINYINT CHECK(score>=0 AND score<=100)
);

Mysql Checking constraints is not supported , It will reduce the speed of data insertion
f、 Foreign key constraints ——FOREIGN KEY

5、 Auto grow Columns AUTO_INCREMENT
AUTO_INCREMENT: Automatic growth , If a column declares a self incrementing column , No manual assignment is required , The assignment is NULL, The current maximum value will be automatically obtained , Then add 1 Insert .
matters needing attention :
Only applicable to integer primary key columns
Allow manual assignment

eg: Primary key 、 Automatic growth 、 Foreign key instance

# Set client-side connection and server-side code 
SET NAMES UTF8;
# Drop the database 
DROP DATABASE IF EXISTS demo;
# Create database 
CREATE DATABASE tedu CHARSET=UTF8;
# Enter the database 
USE demo;
# Create a table to save Department data 
CREATE TABLE dept(
  did INT PRIMARY KEY AUTO_INCREMENT,
  dname VARCHAR(8) UNIQUE
);
# insert data 
INSERT INTO dept VALUES(10,' R & D department ');
INSERT INTO dept VALUES(20,' The Marketing Department ');
INSERT INTO dept VALUES(30,' Operation Department ');
INSERT INTO dept VALUES(40,' Testing department ');
# A table that holds employee data 
CREATE TABLE emp(
  eid INT PRIMARY KEY AUTO_INCREMENT,
  ename VARCHAR(6),
  sex BOOL, #1 male  0 Woman 
  birthday DATE,
  salary DECIMAL(7,2),
  deptId INT,
  FOREIGN KEY(deptId) REFERENCES dept(did)
);
# insert data 
INSERT INTO emp VALUES(NULL,'Tom',1,'1990-5-5',6000,20);
INSERT INTO emp VALUES(NULL,'Jerry',0,'1991-8-20',7000,10);
INSERT INTO emp VALUES(NULL,'David',1,'1995-10-20',3000,30);
INSERT INTO emp VALUES(NULL,'Maria',0,'1992-3-20',5000,10);
INSERT INTO emp VALUES(NULL,'Leo',1,'1993-12-3',8000,20);
INSERT INTO emp VALUES(NULL,'Black',1,'1991-1-3',4000,10);
INSERT INTO emp VALUES(NULL,'Peter',1,'1990-12-3',10000,10);
INSERT INTO emp VALUES(NULL,'Franc',1,'1994-12-3',6000,30);
INSERT INTO emp VALUES(NULL,'Tacy',1,'1991-12-3',9000,10);
INSERT INTO emp VALUES(NULL,'Lucy',0,'1995-12-3',10000,20);
INSERT INTO emp VALUES(NULL,'Jone',1,'1993-12-3',8000,30);
INSERT INTO emp VALUES(NULL,'Lily',0,'1992-12-3',12000,10);
INSERT INTO emp VALUES(NULL,'Lisa',0,'1989-12-3',8000,10);
INSERT INTO emp VALUES(NULL,'King',1,'1988-12-3',10000,10);
INSERT INTO emp VALUES(NULL,'Brown',1,'1993-12-3',22000,NULL);



原网站

版权声明
本文为[Rulyc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101932002944.html