1 in the table, duplicate values may be included. That's not a problem, but sometimes you might want to just list different values (distinct).
Keyword DISTINCT is used to return only different values.
Grammar:
SELECT DISTINCT column name from table name
2 If you want to conditionally select data from a table, you can add a WHERE clause to the SELECT statement.
Grammar
SELECT column name from table name WHERE column operator value
The following operators can be used in the WHERE clause:
Operator description
= equals
<> Not equal to
> Greater than
< less than
>= greater than or equal to
<= less than or equal to
Between within a certain range
Like search for a pattern
3 and and OR operators
And and or can combine two or more conditions in a where sub-statement.
If both the first condition and the second condition are true, the AND operator displays a record.
If only one of the first and second conditions is true, the OR operator displays a record.
The 4 INSERT INTO statement is used to insert a new row into the table.
Grammar
INSERT into table name values (value 1, value 2,....)
We can also specify the columns for which you want to insert data:
INSERT into table_name (column 1, column 2,...) Values (value 1, value 2,....)
The 5 Update statement is used to modify the data in the table.
Grammar:
UPDATE table name SET column name = new value WHERE Column name = value
The 6 DELETE statement is used to delete rows in a table.
Grammar
DELETE from table name WHERE column name = value
The 7 TOP clause is used to specify the number of records to return.
The TOP clause is useful for large tables with thousands of records.
Syntax for SQL Server:
SELECT TOP number|percent column_name (s)
From table_name
8 Like operator
The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.
SQL Like operator syntax
SELECT column_name (s)
From table_name
WHERE column_name like pattern
9 SQL wildcard characters
The SQL wildcard can override one or more characters when searching for data in a database.
SQL wildcard characters must be used with the LIKE operator.
In SQL, you can use the following wildcard characters:
Wildcard description
% replaces one or more characters
_ Replaces only one character
Any single character of [charlist] word columns
[^charlist]
Or
[!charlist]
Any single character that is not in the word columns
The ten in operator allows us to specify multiple values in the WHERE clause.
SQL in syntax
SELECT column_name (s)
From table_name
WHERE column_name in (value1,value2,...)
The between operator is used in the WHERE clause to select a range of data between two values.
Between operator
operator between ... and selects a range of data between two values. These values can be numeric, text, or date.
SQL between syntax
SELECT column_name (s)
From table_name
WHERE column_name
Between Value1 and value2
12 The following lists the JOIN types that you can use and the differences between them.
JOIN: Returns a row if there is at least one match in the table
Left JOIN: Returns all rows from the table, even if there is no match in the right table
Right JOIN: Returns all rows from the correct table even if there is no match in the left table
Full JOIN: Returns a row if there is a match in one of the tables
1. The left JOIN keyword returns all rows from the table (TABLE_NAME1), even if there are no matching rows in the right table (table_name2).
2. The right JOIN keyword returns all rows from the table (table_name2), even if there are no matching rows in the left table (table_name1).
3. The full JOIN keyword returns a row whenever there is a match in one of the tables.
(The full JOIN keyword returns all rows from the left table (Persons) and the right table (Orders).) If the rows in "Persons" do not match in the table "orders", or if the rows in "orders" do not have a match in the table "Persons", the rows are also listed. )
13. SQL UNION operator
The UNION operator is used to combine the result set of two or more SELECT statements.
Note that the SELECT statement inside the UNION must have the same number of columns. The column must also have a similar data type. Also, the order of the columns in each SELECT statement must be the same.
SQL UNION Syntax
SELECT column_name (s) from table_name1
UNION
SELECT column_name (s) from table_name2
Note: By default, the UNION operator chooses a different value. If duplicate values are allowed, use UNION all.
. SELECT into statement
The SELECT INTO statement selects data from one table and then inserts the data into another table.
The SELECT into statement is commonly used to create a backup copy of a table or to archive records.
You can insert all the columns into the new table:
SELECT *
into new_table_name [in Externaldatabase]
From Old_tablename
The following example makes a backup copy of the "Persons" table:
SELECT *
Into Persons_backup
From Persons
The IN clause can be used to copy a table to another database:
SELECT *
Into Persons in ' Backup.mdb '
From Persons
15. Constraints are used to restrict the type of data that joins the table.
You can specify constraints (through the CREATE TABLE statement) When you create the table, or you can (via the ALTER table statement) after the table is created.
We will focus on the following constraints:
(1) Not NULL
The NOT NULL constraint enforces that the column does not accept null values.
A NOT NULL constraint forces a field to always contain a value. This means that if you do not add a value to the field, you cannot insert a new record or update the record.
(2) UNIQUE
Unique constraints uniquely identify each record in a database table.
Both the unique and PRIMARY KEY constraints provide a unique guarantee for a column or column collection.
PRIMARY KEY has a UNIQUE constraint that is automatically defined.
Note that each table can have multiple UNIQUE constraints, but there can be only one PRIMARY KEY constraint per table.
-----ALTER TABLE Persons
ADD UNIQUE (id_p)
----ALTER TABLE Persons
ADD CONSTRAINT Uc_personid UNIQUE (id_p,lastname)
(3) PRIMARY KEY
The PRIMARY KEY constraint uniquely identifies each record in a database table.
The primary key must contain a unique value.
Primary key columns cannot contain NULL values.
Each table should have a primary key, and each table can have only one primary key.
(4) FOREIGN KEY
The FOREIGN key in one table points to the PRIMARY key in the other table.
(5) CHECK
A CHECK constraint is used to limit the range of values in a column.
If you define a CHECK constraint on a single column, the column only allows a specific value.
If a CHECK constraint is defined on a table, the constraint restricts the value in a specific column.
ALTER TABLE Persons
ADD CONSTRAINT Chk_person CHECK (id_p>0 and city= ' Sandnes ')
(6) DEFAULT
The default constraint is used to insert defaults into the column.
If no other value is specified, the default value is added to all new records.
Two
1 Create a unique index on the table. A unique index means that two rows cannot have the same index value.
CREATE UNIQUE INDEX index_name
On table_name (COLUMN_NAME)
2 You can easily delete indexes, tables, and databases by using the DROP statement.
The 3 ALTER Table statement is used to add, modify, or delete columns in an existing table.
4 SQL ALTER TABLE syntax
To add columns to a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column from a table, use the following syntax:
ALTER TABLE table_name
DROP COLUMN column_name
Note: Some database systems do not allow this method of deleting columns in a database table (DROP column column_name).
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
MySQL add columns, modify column names, column properties, delete column statements
MySQL Modify table name, column name, column type, add table column, delete table column
ALTER TABLE test rename test1; --Modify Table name
ALTER TABLE test Add column name varchar (10); --Add Table columns
ALTER TABLE test drop column name; --Delete Table columns
ALTER TABLE test Modify address char (10)--Modify the form column type
|| ALTER TABLE test change address address char (40)
ALTER TABLE test Change column address Address1 varchar (30)--Modify table column name
Database Knowledge Summary